+ 1

(Solved) Lesson 28.2 - What’s My Discount - Can somebody explain me the logic of the code?

Hey everyone! 😁 Is anyone here who can explain me the logic behind the code? (Why is this correct?) I’m not sure, whether I understand it correct or not. Code: #include <iostream> using namespace std; int main() { double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35}; int p; for (int x = 0; x <11 ; x++) { cin >> p; cout << items [x] - items [x]*p/100 << " "; } return 0; } First question: Why is it “items [x] - items [x] “ ? Thank you so much!! 🤝

16th Jun 2022, 5:28 AM
Jeremy
Jeremy  - avatar
10 Answers
+ 5
Jeremy Your question was why? items[x] - items[x] ? This is not like that as you are thinking You missed items[x] * p / 100 We are subtracting discount from the main amount So you have to think that expression like this: items[x] - (items[x] * p / 100) items[x] => main amount items[x] * p / 100 => discount amount discounted_amount = main_account - discount_amount
16th Jun 2022, 5:28 PM
A͢J
A͢J - avatar
+ 4
In my previous response, I broke the expression into parts, the parentheses were there to show you which parts of the expression was evaluated prior to the other parts. item[ x ] - ( ( item[ x ] * p ) / 100 ) Multiplication of item[ x ] with <p> first. The multiplication result is then divided by 100, which gives the price (item[ x ]) subtracted by <p> percent. The division result is then used to subtract the original price (item[ x ]), to get the discounted price. I think AJ was right to say that <p> is only read once, before the loop began. Use of array is purpose of the lesson, in order for us to understand how to fetch an element's value from it, using an index. Also, array is there because the discount was supposed to be applied to multiple different product prices. Arrays are meant for storing multiple values all of the same type, in this case, product prices.
16th Jun 2022, 12:05 PM
Ipang
+ 3
Jeremy x - x * 10 / 100 Here * has more priority than minus (-) so first calculation will be on (x * 10 / 100) then this value will be subtract by x so x - (value of (x * 10 / 100)) ------ one more example: 100 - 100 * 2 = 100 - 200 = -100
16th Jun 2022, 2:29 PM
A͢J
A͢J - avatar
+ 3
A͢J Thank you. :) Yes, I ubderstand. I don‘t know why but for some reason I got the point while reading the text from Ipang :‘) !! Your last text would definitely have helped me a lot at the beginning to understand the minus correctly. 😅🤝 … sometimes I‘m totally stumped. 🫠 So… thanks again everyone!! :)
16th Jun 2022, 5:40 PM
Jeremy
Jeremy  - avatar
+ 2
Jeremy So suppose x = 100 now you want to get 20% discount from x amount so => x - 20 * x / 100; Now items[x] - items[x] * p / 100 it is same like above example Here we have to get p% of each item so here items[x] means each item of the given array. p% of each item is = items[x] * p / 100 now we want discounted amount, so we have to subtract discount amount from each items so items[x] - items[x] * p / 100
16th Jun 2022, 5:47 AM
A͢J
A͢J - avatar
+ 2
Jeremy cin >> p should be outside the loop because we have to take once
16th Jun 2022, 5:52 AM
A͢J
A͢J - avatar
+ 2
Multiplication and division operation takes precedence over subtraction. On that line we see multiplication and division, and since multiplication and division has similar precedence, the evaluation order will be left to right. So, taking the first value from <items> array (500), and 10 as <p> value, the line may be evaluated as follows item[ x ] - ( ( items[ x ] * p ) / 100 ) 500 - ( ( 500 * 10 ) / 100 ) 500 - ( 5000 / 100 ) 500 - 50 Then we get 450 as final result.
16th Jun 2022, 5:56 AM
Ipang
+ 1
Thanks A͢J & Ipang . :) But… I don‘t know why sololearn want the p inside the loop. :/ Hmm… I‘m still don‘t understand, why its minus („items [x] - items [x]“). And why its important to use an array? @.@ And isn‘t it items [x] - (items [x] *p/100) ? Thanks in advance!! 🤝
16th Jun 2022, 11:12 AM
Jeremy
Jeremy  - avatar
+ 1
Thank you Ipang !! 🤝
16th Jun 2022, 2:16 PM
Jeremy
Jeremy  - avatar
+ 1
A͢J Yes, I know. But this was not my question. 😅 But now it’s alright!! Thank you for your time!! :) 🤝
16th Jun 2022, 4:53 PM
Jeremy
Jeremy  - avatar