[SOLVED] Why my output is anyway 50? | Sololearn: Learn to code for FREE!

+1

[SOLVED] Why my output is anyway 50?

My exersice say: You are working on a ticketing system. A ticket costs \$10. The office is running a discount campaign: each group of 5 people is getting a discount, which is determined by the age of the youngest person in the group. You need to create a program that takes the ages of all 5 people as input and outputs the total price of the tickets. Sample Input: 55 28 15 38 63 Sample Output: 42.5 The youngest age is 15, so the group gets a 15% discount from the total price, which is \$50 - 15% = \$42.5 My code is: #include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } //your code goes here int youngest = ages[0]; for (int a = 0; a <5; ++a) { if(youngest>ages[a]) { youngest = ages[a]; } } int prezzo = 50 - 50/100 * youngest; cout << prezzo; return 0; } /*My output is 50 and I don't understand why. I try many solution*/

12/25/2020 8:27:30 AM

Alexander Thomson

+1

Alexander Thomson In your case 50/100 will give 0. That's why output is 50 - 0 = 50 To get in decimal you need to cast int to double or do like this double prezzo = 50.0 - 50.0/100.0 * youngest;

+8

Liza Cali this is the solution founded (is working) CODE: #include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } double youngest = ages[0]; for (int a = 0; a <5; ++a) { if(youngest>ages[a]) { youngest = ages[a]; } } double prezzo = 50 - (50*youngest/100); cout << prezzo; return 0; }

+3

Aysha Simra yea now is all clear, I understand my mistake 😁

+3

Ankit Kumar Wrong place. Noone is going to give you answer. And btw you can check on Code Playground.

+3

LUCI- FOR mustafa khlid Doyal Roy مهدي حميد https://www.sololearn.com/discuss/1316935/?ref=app

+2

Alexander Thomson Yes

+2

Alexander Thomson Check this again double prezzo = 50.0 - 50.0/100.0 * youngest;

+2

I Am Groot ! Sandeep Shaw Aysha Simra Thanks all you so much for helping me 😁

+2

Your most welcome Alexander Thomson hope it's clear to you now how to solve this problem

+2

That's great Alexander Thomson keep learning

+2

ALİCAN DİLEK don't spam here

+2

Liza Cali She already solved the problem. Youngest should be int not double

+2

Great job Alexander!😄

+2

Sorry I’m blind. I didn’t see that youngest was declared “double” 😜

+1

Try using this method prezzo = 50 - (50*youngest/100);

+1

Type Conversion might be the solution Use ` double prezzo ` 0.5 is converted to 0 in ` int `

+1

Ok.. I don't understand what happened.. I try to change the method Prezzo = 50 - (50*yoingest/100); The result is near to be correct because the output can be decimals number. So I change the integer to double and it work correctly. Now the question is why my method don't work? Before to post I have yet try to do double prezzo = 50 - (50/100*youngest) But that still output 50 or 0 I don't remember

+1

I Am Groot ! I tried double b = 50 - 50 / 100 * youngest; but don't work, maybe the problem is I don't have write 50.0?

+1

It should be int x= 50- (50/100) int prezzo = x * youngest;

+1

<redacted>