+ 1

# why c++ code not satisfy all test cases

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 for this above problem I write a c++ code in three methods Method 1: #include <iostream> using namespace std; int main() { int ages[5]; float min,p,t; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } min=ages[0]; for(int j=1 ; j < 5; j++) { if(min>ages[j]) { min=ages[j]; } } p=(min/100)*50; t=50-p; cout<<t; return 0; } Method 2: #include <iostream> using namespace std; int main() { int ages[5]; float min,p,t; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } min=ages[0]; for(int j=1 ; j < 5; j++) { if(min<ages[j]) { break; } else { min=ages[j]; } } p=(min/100)*50; t=50-p; cout<<t; return 0; } Method 3: #include <iostream> using namespace std; int main() { int ages[5]; float min,p,t; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } if(ages[0]<ages[1]&&ages[0]<ages[2]&&ages[0]<ages[3]&&ages[0]<ages[4]) { min=ages[0]; } if(ages[1]<ages[0]&&ages[1]<ages[2]&&ages[1]<ages[3]&&ages[1]<ages[4]) { min=ages[1]; } if(ages[2]<ages[1]&&ages[2]<ages[0]&&ages[2]<ages[3]&&ages[2]<ages[4]) { min=ages[2]; } if(ages[3]<ages[1]&&ages[3]<ages[2]&&ages[3]<ages[0]&&ages[3]<ages[4]) { min=ages[3]; } if(ages[4]<ages[1]&&ages[4]<ages[2]&&ages[4]<ages[3]&&ages[4]<ages[0]) { min=ages[4]; } p=(min/100)

12th Mar 2021, 11:50 AM
+ 2
I pasted your first code into the exercise and it passed all cases, so if it doesn't work for you, you might want to mail info@sololearn.com about it. From what I can see, there shouldn't be anything wrong with it. The second one is incorrect because you potentially break from the loop too early, and the third version is cut off and therefore not entirely judgeable, although I would say you will get into trouble if two or more persons share the youngest age, given that the conditionals only account for a strict ordering. I would suggest you to create your codes in the playground and link those in the future when asking questions here, both for the sake of shorter descriptions and better accessibility of the code.
12th Mar 2021, 12:48 PM
+ 2
Anwar Badurudeen Try this: #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; } Happy coding šš
12th Mar 2021, 8:47 PM
ā¤ļøšPreranašā¤ļø
0
Post the full description.. which code is not working..? Is output required is 50*15/100=7.5 50-7.5=42.5
12th Mar 2021, 12:54 PM
Jayakrishna š®š³
0
Thanks for the reply and suggestions " shadow " . And yes for me ,also the first method passed all cases . All I want to know that why the remaining methods(2&3) does not pass all cases . For that only I raised this question and I think the above question does not show my full description. and once again thanks shadow for clarifying my doubt and for you suggestion I will follow it for my future questions :)
12th Mar 2021, 4:51 PM