Arrays in a loop question | Sololearn: Learn to code for FREE!

+2

Arrays in a loop question

Allow me to enter 2 different C++ codes before I ask the question // 1ST CODE #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; } // 2ND CODE #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] * discount) / 100; cout << discounted << " "; } return 0; } As you can see, at the bottom of the code. There is a difference. One subtracts items[x] and the other doesn't. Why is that? The first one is correct but why?

c++

9/22/2021 3:13:01 AM

Malik Mehrab Rashid

12 Answers

New Answer

+8

Example consider 500 is the price and discount is 25. (500 * 25)/100 is the discount value and you should subtract it from 500 for the discounted price..

+4

Thank you very much! I've been so confused over this. Thank you for clearing it up! 🙂

+4

Hi... You are in a store, buying something. After choosing some parts, you need to pay, of course. You ask for a discount and the attendant says: the total is 100, I can give you 15% off. Cash discount = $15 (this is the output of the second code: Total price * discount). But! Is it the total of $15 you are going to pay? Of course not. So you ask: Ok, how much is the total amount I will pay for this? Attendant do the math: The total in $ is 100 Discount in% 15 (or 0.15) Therefore: $100 * 0.15 = $15 Again: this is the output of the second code Now you need to know the total to pay: $100 - $15 = $85 Or $100 - ($100 * 0.15)

+2

+2

it depends on ur 2 out put value both the casual status is different so for profit u should cascade both

+1

I mean what is the desired output of the code?, The discounted value for both programs are different so one should be correct and the other goes wrong.....what do you expect to output in the program??

+1

Well they are asking the price of discounted items. Your code finds the discount value but you should subtract the discount price from the actual price then only it becomes discounted price.

0

What's the question for the code??

0

It's at the very bottom. I'll repeat it. There is a difference near the end of the codes. one subtracts items[x] and the other doesn't. Why is that? why is the first one correct and the 2nd one doesn't display the correct discounted price?

0

I've pasted the original question below. The first code I provided above is correct and the 2nd code doesn't give the correct output. I found the answer on someone's post and the only difference they had was they added items[x] - items[x] but I didn't. Why do we take that away? Why are the outputs different? 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

0

You can also find the original practice question on sololearn by going to practice 29.2 on C++

0

If we talk about discount, it's a decrease in the price of a product or service. You find the discount by multiplying the percentage of discount given by the original price. And then subtract the amount you got from the original price. Eg: If original price is 1000 and discount is 55% Discount amount= 1000 * (55/100) = 550 Discounted amount = 1000 - 550 = 450 The amount to be paid now is 450 GF