- 1

Ticket office C++

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 MY CODE: #include <iostream> using namespace std; int main() { int ages[5]; for(int i=0; i<5; i++){ cin >> ages[i]; } int temp = ages[5]; for(int i=0; i<5; i++) { if(temp>ages[i]){ temp = ages[i]; } } float d = 50*(temp/100.00); float total = 50-d; cout << "

quot; << total; } Whats problem. Output comes $50. but when i run it on PC it runs.

21st Jan 2021, 2:28 PM
MD. Omar Faruk Maruf
MD. Omar Faruk Maruf - avatar
1 Respuesta
+ 1
(int temp = ages[5]) ages[5] it's not an element of ages, the last element of ages is ages[4] This will result in an undefined behavior
21st Jan 2021, 11:55 PM
Angelo
Angelo - avatar