0

C++ Ticket Office - Can't find the mistake: Only Test Case 2 fails, all others pass

#include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } int discount; int a = ages[0]; int b = ages[1]; int c = ages[2]; int d = ages[3]; int e = ages[4]; if (a<=b&&c&&d&&e) {discount = a;} if (b<=a&&c&&d&&e) {discount = b;} if (c<=b&&a&&d&&e) {discount = c;} if (d<=b&&c&&a&&e) {discount = d;} if (e<=b&&c&&d&&a) {discount = e;} double price = 50-50*discount*0.01; cout << price; return 0; }

11th Jun 2021, 12:09 AM
flippo
2 Respuestas
+ 4
a<=b&&c&&d&&e (and the others similarly) Isn't really valid the way you want it to work. In this case if b,c,d,e are non zero numbers then they will return true, regardless if they are greater than or equal to a. If they are equal to 0 then they will be false. You would need to perform the comparison on each variable like; a<=b && a<=c && a<=d && a<=e (Likewise with the others) A better way to accomplish this task would be to set the discount to the 1st element of the ages array (ages[0]), then loop over the remaining elements and check if the current element is less than the current value of discount. If true then set discount to the current element. int discount = ages[0]; for (int i = 1; i < 5; i++) { if (ages[i] < discount) { discount = ages[i]; } }
11th Jun 2021, 1:47 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@ChaoticDawg Thank you very much! Yes, the code looks way better now and works. Thanks!
11th Jun 2021, 7:04 AM
flippo