+ 2

In the test Tickets Ofice I did this code but test case 3 is hidden, so can you please tell wath's wrong with my code. Here :

#include <iostream> using namespace std; int main() { float ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; }float yongest ; if (ages[0]<ages[1]){ if (ages [0]<ages[2]){ if (ages[0]<ages[3]){ if (ages[0]<ages[4]){ yongest= ages[0]; } } } } if (ages[1]<ages[0]){ if (ages [1]<ages[2]){ if (ages[1]<ages[3]){ if (ages[1]<ages[4]){ yongest= ages[1]; } } } } if (ages[2]<ages[1]){ if (ages [2]<ages[0]){ if (ages[2]<ages[3]){ if (ages[2]<ages[4]){ yongest= ages[2]; } } } }//I did the same thing with ages[3]and ages [4] float price; price =50.00-(50.00* yongest /100.00); cout<< price ; return 0; }

20th Dec 2020, 8:19 PM
sofia badri
sofia badri - avatar
3 ответов
+ 2
Maybe type of "ages" variable is problem, I guess it should be int, everything else looks fine, but here you go with easier to implement version: #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 low = ages[0]; for (int i =0; i < 5; i++){ if(low > ages[i]) low = ages[i]; } double price = 50.0; cout<<(price*(1.0-(double)low/100.0)); return 0; } At least you can get rid of these if statements by using for loop to search for the youngest
21st Dec 2020, 1:33 AM
Michal Doruch
+ 1
Thanks, the only thing is that in the for loop (int i =0) must be int i =1
21st Dec 2020, 10:10 AM
sofia badri
sofia badri - avatar