0

ticket office problem

MY CODE IS NOT WORKING FOR TEST CASE 4 ONLY 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 The given code is declaring an array of 5 elements and taking them from input using a loop. #include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } int e =0; double total=50.0; int u=0; for (int y =0; y < 5; y++){ if (ages[0] < ages[u] && ages[0] <= ages[0]){ e =ages[0]; } else if (ages[1] < ages[u] && ages[1] <= ages[0]){ e =ages[1]; } else if (ages[2] < ages[u] && ages[2] <= ages[0]){ e =ages[2]; } else if (ages[3] < ages[u] && ages[3] <= ages[0]){ e =ages[3]; } else if (ages[4] < ages[u] && ages[4] <= ages[0]){ e =ages[4]; } else{ e=ages[y]; } u++; } double t= e*total/100; double r = 50 - t; cout<<r; return 0; }

30th Aug 2022, 1:04 AM
The boss
The boss - avatar
1 Answer
+ 2
the cause of error is finding wrong lowest age. why are you doing roughly about 50 comparisons to find the minimum? let's assume e = ages[0] then go from ages[1] to ages[4] to see if any of them is smaller than e. test cases that pass are due to their arrangement, but suppose you get: ages = {5,3,9,7,8} your loop gives 5 as minimum because ages[0] < ages[u] when u = 0 is false therefore e = 0 ages[1] < ages [u] is false this goes until e = ages[y] that is e = 5 now u is 1 ages[0] < ages[u] is false (5 < 3) ages[1] < ages[u] is false (3 < 3) ... when u is 0 first if is always false when u is 1 second if is always false etc. hope this helps.
30th Aug 2022, 3:51 AM
Tina
Tina - avatar