why c++ code not satisfy all test cases | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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
Anwar Badurudeen
Anwar Badurudeen - avatar
7 Answers
+ 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 [email protected] 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
Shadow
Shadow - avatar
+ 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😍❤️
❤️😍Prerana😍❤️ - avatar
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
Anwar Badurudeen
Anwar Badurudeen - avatar
0
From Methods Method 1 is complete and gives correct result for any input OUTPUT Method 1 50-(15*50/100) = 50 - 7.5 = 42.5 Method 2 is gives incorrect result for given input, it can give in correct results if minimum value is at first position Output Method 2 50-(28*100/50) = 50-14.0 = 36.0 Method 3 is correct and gives correct result for all kind of input OUTPUT Method 3 50-(15*50/100) = 50-7.5 = 42.5 DHANANJAY
12th Mar 2021, 5:12 PM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
0
It is good coding practice to use meaningful words for programming. Your program is very easy to grasp because it has meaningful words as variable name. Have a Good luck🤔🌝
12th Mar 2021, 10:22 PM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
0
Here is the wright answer https://www.sololearn.com/coach/904?ref=app Try it.
13th Mar 2021, 4:45 AM
Aditya Partap Singh
Aditya Partap Singh  - avatar