arrays in a 'for' loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

arrays in a 'for' loop

Hi guys! So I'm stuck again on a challenge: 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 (" ") here is my code, which obviously only gives me the calculation for the last price, because I can't figure out the right way to output results in requested order #include <iostream> using namespace std; int main() { double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35}; int discount; double finPrice; cin>>discount; for (int x=0; x<11; x++) { finPrice=items[x]-(items[x]*discount)/100; } cout << finPrice << endl; return 0; } https://www.sololearn.com/learning/1051/1628/2852/2 Thanks in advance!

20th Jan 2021, 5:34 PM
Valerie S
Valerie S - avatar
18 Answers
+ 9
#include <iostream> using namespace std; int main() { double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35}; int discount; double finPrice; cin>>discount; for (int x=0; x<11; x++) { finPrice=items[x]-(items[x]*discount)/100; cout << finPrice << " "; } return 0; }
20th Jan 2021, 6:07 PM
JaScript
JaScript - avatar
+ 5
Congrats and happy coding furthermore.
20th Jan 2021, 8:47 PM
JaScript
JaScript - avatar
+ 4
Hints: double finPrice[11]; Make the "finPrice" an array so it will store each item's discounted price. Then use the "for loop" to iterate each item and insert each discounted prices of items to "finPrice" array. Finally, use "for loop" to iterate each discounted items from "finPrice" array then print each followed by spaces. The code is up to you. If you're still stuck, please feel free to ask. Thanks and Good luck!
20th Jan 2021, 6:00 PM
noteve
noteve - avatar
+ 3
Valerie S Typo Error in mine It is "finPrice" array not "discount". Sorry, a mistake.
20th Jan 2021, 6:11 PM
noteve
noteve - avatar
+ 3
oh, that makes more sense, thanks!
20th Jan 2021, 6:13 PM
Valerie S
Valerie S - avatar
+ 3
omg, somehow my version of the same code as JaScript suggested is not working at all, I've checked everything three times >.< Update: it works, i was just being stupid
20th Jan 2021, 6:21 PM
Valerie S
Valerie S - avatar
+ 2
thank you sm! gonna try to wrap my head around this for now
20th Jan 2021, 6:05 PM
Valerie S
Valerie S - avatar
+ 2
>.<
22nd Jan 2021, 5:51 AM
Ramna Ramna
Ramna Ramna - avatar
+ 2
#include <iostream> using namespace std; int main() { double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35}; //your code goes here float discount; float discounted; cin >> discount; for (int x = 0; x < 11; x++) { discounted = items[x] - items[x] * discount / 100; cout << discounted << " "; } return 0; }
22nd Sep 2021, 3:37 AM
Malik Mehrab Rashid
Malik Mehrab Rashid - 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 num ; cin>>num; //your code goes here for(int i=0;i<11;i++) cout<<items[i]-items[i]*num/100<<" "; return 0; }
13th Oct 2021, 9:43 PM
Hasan Ali
Hasan Ali - avatar
+ 1
A way easier solution is this. #include <iostream> using namespace std; int main() { double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35}; //your code goes here int p; int x; cin>>p; //where p is the input discount do{ cout<<items[x]-(items[x]*p)/100<<" "<<endl; //note: the <<endl can be removed if necessary. x++; } while (x<11); return 0; }
13th Jun 2023, 10:00 PM
Avalon
Avalon - avatar
0
I dont get it ..why we are not using x<=11 instead of x<11...and why x<=11 s showing error?
20th Apr 2021, 3:39 PM
Premium World
0
Can anyone explain that if total is 1947.3 .. then discount of 30% should be 1363.11 but it shows 24.5..any idea why?
19th Jun 2021, 3:42 PM
Sujuma
Sujuma - avatar
0
When postings please do well to post every last info 🙏🙏
23rd Aug 2021, 12:53 AM
Nana King Obedson
Nana King Obedson - avatar
0
#include <iostream> using namespace std; int main() { double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35}; //your code goes here int a; cin>>a; double discounted; for(int i=0;i<11;i++) { discounted=items[i]-a*items[i]/100; cout<<discounted<<" "; } return 0; } Explanation: I use this formula because it's easy to understand items[i]-a*items[i]/100 Same as>> nums - perc% Let say: The value of nums is 500 And the sample percentage input is 25 To get the percentage we need to multiply 500 by 25 and divide it into 100 >>(500*25/100) the answer is 125 then reduce 500 by 125 500-125=375 Done!☑️
4th Jan 2022, 1:20 AM
Ken-Ken Dumato
Ken-Ken Dumato - avatar
0
#include <iostream> using namespace std; int main() { double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35}; double percentageOff; cin>>percentageOff; percentageOff = percentageOff/100; //your code goes here for(int x = 0; x < 11; x++){ cout<<items[x]*(1-percentageOff)<<" "; } return 0; }
18th Jan 2022, 4:13 AM
Damna Liana
Damna Liana - 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 discount; double finPrice; cin>>discount; for (int x=0; x<11; x++) { finPrice=items[x]-(items[x]*discount)/100; cout << finPrice << " "; } return 0; } Good Luck
25th Jan 2022, 4:34 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
#include <iostream> using namespace std; int main() { double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35}; //your code goes here double discount; cin>> discount; double equ; for(int i=0; i<=10;i++ ) { equ=items[i] - discount*(items[i]/100); cout<<equ<<" "; } return 0; }
8th Dec 2022, 1:37 PM
anas safi