+ 2
āWhatās My Discount?ā
"You are given an array of doubles of items in a store that have different prices (see template). Write a program that takes the "percent off" discount as input and outputs discounted item prices on one line in the same sequence you are given, separated by a space (" "). Sample Input 25 Sample Output 375 9.3 70.5 33.75 2.25 60.75 750.675 63.75 67.5 0.75 26.25ā āTo get a discounted price use the formula: a ā a*p/100, where a is the initial price and p is the discount percent off.ā https://code.sololearn.com/ci3II727cn50/?ref=app
14 Answers
+ 3
'i' is the index counter
'a' should be double as prices are stored in double array 'items'
price is got from array 'items', by doing a = items[i]; inside the loop.
index ('i') must be int
'cost' should be double also, as expected output should handle floating numbers
if there is the last item missing, it is because index should be tested against array length, not array length-1 (or test for less than or equal): update done in my code (changing i<10 to i<11 -- you could also do i<=10)
+ 2
your code with corrections (old code in comments):
https://code.sololearn.com/cXQ7FTCGEv8k/?ref=app
+ 1
int are integers (whole numbers)
double stand for double precision float, wich are floating point numbers ;)
+ 1
#include <iostream>
using namespace std;
int main() {
double items[11] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35};
int a;
cin >> a;
for (int i=0;i<11;i++) {
cout << items[i]*(100-a)/100 << " ";
}
return 0;
}
// Hope this helps
+ 1
i as first letter of 'index'
but also, because that's the common name for counter variables... (inside nested loop, we often use j, then k...)
you could name it as you want: index, item, foo, bar, x, n... better practice to use meaningfull name ;)
+ 1
Tahitiš· That's just for convenience; '[i]' or '[x]' looks much better than '[a]' (for me).
+ 1
#include <iostream>
using namespace std;
int main() {
double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35};
double discount;
cin>> discount;
for(int x= 0;x<11;x++){
cout<< " "<< items[x] - items[x]* discount/100;
}
return 0;
}
0
visph ,
Thank you. Iām having trouble understanding the corrections.
When I ran your code, the output was the same initializer list, but missing the last number (500 12.4 94 45 3 81 1000.9 85 90 1). The output is supposed to result in discounted prices.
Where I typed āint aā, are you saying I should have instead typed ādouble aā? Also, ādouble costā instead of āint costā? And why the variable āiā instead of āaā? Thanks again.
0
if you give 25 as input in "my" code, you'll get the expected result...
the only way to get the same as initial list is to give 100 as input ^^
0
visph ,
Ok. Thank you very much. I need to study a lot more on arrays, especially the ādoubleā concept. Thanks for your help!
0
Thank you, Calvin Sheen Thomas .
This one is really mind-boggling for me. Why donāt you use āpā as a variable and as the input? The instructions say that āpā represents the discount percentage, and that the discount percentage should be given as input. You and visph both use āiā as the index counter. Is it a standard rule to use āiā, or do you simply choose āiā because itās the first letter in the word āitemsā?
I know I need to revisit the tutorial. Thanks again for helping!
0
Thatās very helpful advice, visph . Merci beaucoup !
0
#include <iostream>
using namespace std;
int main() {
double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35};
int p;
cin >> p;
//your code goes here
for (int x = 0; x < 11; x++){
items[x] = items[x]-((items[x]*p)/100);
cout << items[x] << " ";
}
return 0;
}
- 2
#include <iostream>
using namespace std;
int main() {
double o[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35};
float b;
cin>>b;
for(int x=0;x<11;++x){
o[x]-=o[x]*b/100;
cout<<o[x]<<" ";
}
return 0;
}