[SOLVED] Why my output is anyway 50? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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*/

25th Dec 2020, 8:27 AM
Alexander Thomson
Alexander Thomson - avatar
56 Réponses
0
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;
25th Dec 2020, 9:32 AM
AÕĘJ
AÕĘJ - avatar
+ 13
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; }
25th Dec 2020, 12:26 PM
Alexander Thomson
Alexander Thomson - avatar
+ 9
#include <iostream> using namespace std; int main() { // Group of 5 peoples is declared here// int ages[5]; // A for loop for looping 5 indexes for group // for (int i = 0; i < 5; ++i) { cin >> ages[i]; // Input will be taken in 5 looped indexes } // Input has been taken here // //your code goes here /* here we declared a variable named youngest as double which will be equal to the first input entry of ages group */ double youngest = ages[0]; /* Another loop for inputed ages group to detect whether youngest is really youngest or not */ // it will just compare youngest with every entry // for (int a = 0; a <5; ++a) { // comparing youngest over every entry // if(youngest>ages[a])/* If youngesy is greater than a corresponding entry */ { youngest = ages[a]; // Youngest == That Entry. // Means That enciuntered entry will be real youngest. // We declared that entry as younngest. } } // Rest is just the simple Maths // double discont = 50 - (50*discount/100); cout << discount; return 0; }
26th Sep 2021, 5:34 PM
Govind Ramchandra Kulkarni.
Govind Ramchandra Kulkarni. - avatar
+ 2
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
25th Dec 2020, 8:52 AM
Alexander Thomson
Alexander Thomson - avatar
+ 2
Sorry I‚Äôm blind. I didn‚Äôt see that youngest was declared ‚Äúdouble‚ÄĚ ūüėú
25th Dec 2020, 3:34 PM
Cali
Cali - avatar
+ 2
The Outputs Are working #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; }
8th May 2021, 11:07 AM
Verjin V
Verjin V - avatar
+ 1
can anyone explain how to calculate that discount thing?
24th Aug 2021, 5:09 AM
GURURAJ KL
GURURAJ KL - avatar
+ 1
#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; } Good Luck
13th Dec 2021, 2:25 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
+ 1
#include <iostream> using namespace std; int main() { int arr[5]; for (int i = 0; i < 5; ++i) { cin >> arr[i]; } //your code goes here int min=arr[0]; for(int i=0; i<5;i++) { if(arr[i]<min) { min=arr[i]; } } int tickets_Price=10; int size=sizeof(arr)/sizeof(arr[0]); int price= tickets_Price*size; double discount= price*min*0.01; double final_Price= price-discount; cout<< final_Price; return 0; }
21st Jun 2022, 10:54 PM
yoyomaybe
yoyomaybe - avatar
0
Type Conversion might be the solution Use ` double prezzo ` 0.5 is converted to 0 in ` int `
25th Dec 2020, 8:35 AM
Sandeep Shaw
Sandeep Shaw - avatar
0
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?
25th Dec 2020, 9:46 AM
Alexander Thomson
Alexander Thomson - avatar
25th Dec 2020, 9:47 AM
AÕĘJ
AÕĘJ - avatar
0
Alexander Thomson Check this again double prezzo = 50.0 - 50.0/100.0 * youngest;
25th Dec 2020, 9:49 AM
AÕĘJ
AÕĘJ - avatar
0
I Am Groot ! Sandeep Shaw Aysha Simra Thanks all you so much for helping me ūüėĀ
25th Dec 2020, 10:05 AM
Alexander Thomson
Alexander Thomson - avatar
0
Aysha Simra yea now is all clear, I understand my mistake ūüėĀ
25th Dec 2020, 10:27 AM
Alexander Thomson
Alexander Thomson - avatar
0
Alexander Thomson Keep learning You are going great
25th Dec 2020, 10:56 AM
Sandeep Shaw
Sandeep Shaw - avatar
0
Liza Cali if I begin the second loop with 1 the loop will be executed only 4 time for(int a=1; a<5 ;++a) About the error is yet found and is not the syntax but the type of variable I used. In this case we need a double variable and not a integer. Because during the calculations we have a decimals number and the final valor of the variable can be a decimal number ūüėĀ
25th Dec 2020, 11:34 AM
Alexander Thomson
Alexander Thomson - avatar
0
Liza Cali She already solved the problem. Youngest should be int not double
25th Dec 2020, 12:26 PM
AÕĘJ
AÕĘJ - avatar
0
Great job Alexander!ūüėĄ
25th Dec 2020, 3:32 PM
Cali
Cali - avatar