+ 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

9th Mar 2021, 12:40 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
14 odpowiedzi
+ 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)
9th Mar 2021, 1:56 AM
visph
visph - avatar
+ 2
your code with corrections (old code in comments): https://code.sololearn.com/cXQ7FTCGEv8k/?ref=app
9th Mar 2021, 12:58 AM
visph
visph - avatar
+ 1
int are integers (whole numbers) double stand for double precision float, wich are floating point numbers ;)
9th Mar 2021, 2:11 AM
visph
visph - avatar
+ 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
9th Mar 2021, 9:21 AM
Calvin Thomas
Calvin Thomas - avatar
+ 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 ;)
10th Mar 2021, 2:38 AM
visph
visph - avatar
+ 1
Tahiti🍷 That's just for convenience; '[i]' or '[x]' looks much better than '[a]' (for me).
10th Mar 2021, 4:47 AM
Calvin Thomas
Calvin Thomas - avatar
+ 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; }
18th Dec 2021, 12:58 AM
David Zippel
David Zippel - avatar
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.
9th Mar 2021, 1:33 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
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 ^^
9th Mar 2021, 2:01 AM
visph
visph - avatar
0
visph , Ok. Thank you very much. I need to study a lot more on arrays, especially the “double” concept. Thanks for your help!
9th Mar 2021, 2:09 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
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!
10th Mar 2021, 2:34 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
That’s very helpful advice, visph . Merci beaucoup !
10th Mar 2021, 2:51 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
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; }
5th Mar 2023, 11:21 AM
Nája
Nája - avatar
- 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; }
28th May 2021, 12:26 AM
youssef
youssef - avatar