0

# C++ Course 3 Topic, Data Types, Arrays, Pointers, Practice

Good Evening programmers At now i am trying to resolve the final practice exerscice, but i´m having problems with it, the problem says the following : "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 I am going to show my code for the problem : #include <iostream> #include <iomanip> using namespace std; int main() { int ages[5]; float sum ; int aux, x; for (int i = 0; i < 5; i ++) { cin >> ages[i]; if( ages[i] > ages[i-1] && i >= 1) { aux = ages[i]; ages[i] = ages [i-1]; ages[i-1] = aux; x = i; } } cout << ages[x]; sum = 50 - (50 * (ages[x] / 100.0)); cout<<"The total price is equal to " << sum << endl; return 0; } In fact it works for the four first test , but it is failing with the five and ultimate text (and i can´t the inputs, cause i am not premium, i only want to see the result in all test is correct). Can you help me ? , i am glad for this.

17th Mar 2021, 5:13 PM
Mario Iglesias
6 ответов
+ 1
I already included a concrete example of when your code fails earlier. However, your idea of the smallest value ending up at the end is correct. The problem is that you update 'x' only if a swap occurs, which can lead to 'x' not referring to the last index/ element of the array, i.e. in situations where no swap occurs for the last element. Since you can guarantee that the smallest element will be at the end of the array, you don't need 'x', you can just work directly with the last element.
17th Mar 2021, 6:02 PM
0
I don't quite understand the idea behind your conditional, but I can tell it's not sufficient to always determine the smallest age correctly. For example, consider the input 5 4 3 2 1 where the condition never becomes true and 'x' is left in an undefined state. If you want to take the input while determining the lowest age, I would suggest using a variable to store the lowest age and initially setting it to a sufficiently high value, e.g. int min{ ~( 1 << 31 ) }; Then you can compare "min" to the currently read input and update its value if it is bigger. Note you are supposed to only output the final price, nothing else, and some inputs will always be locked, regardless of PRO status, to prevent hardcoding of the test cases.
17th Mar 2021, 5:26 PM
0
you could initiaiize x with first age and update its value as soon as a lesser age is entered ;)
17th Mar 2021, 5:28 PM
visph
0
#include <iostream> #include <iomanip> using namespace std; int main() { int ages[5]; float sum ; int min =150; //max age for (int i = 0; i < 5; i ++) { cin >> ages[i]; if( ages[i] < min ) //if current input is smaller than min value min = ages[i]; // make it our new min value } if(min>100) min=0; //otherwise sum will be negative sum = 50 - (50 * (min / 100.0)); cout<<"The total price is equal to " << sum << endl; return 0; }
17th Mar 2021, 5:39 PM
deleted
0
Thank all of us by your answer, but my code works , in general, with many imputs, at all i have passed the 4 of 5 test cheks. I am goint to explain, y make a for loop , that in each interation evalues the "i " element in the array, and if if this value is mayor than the precedent element in array, then change the position. Because this change the absolute minor value is always at the end of the array , and i can make the discount then. I don´t know if you understand my english or i explained correctly. I dont understand how my code works in general but have fails.
17th Mar 2021, 5:48 PM
Mario Iglesias
0
Now it works , and passed all test cheking ! #include <iostream> #include <iomanip> using namespace std; int main() { int ages[5]; float sum ; int aux; for (int i = 0; i < 5; i ++) { cin >> ages[i]; if( ages[i] > ages[i-1] && i >= 1) { aux = ages[i]; ages[i] = ages [i-1]; ages[i-1] = aux; } } sum = 50 - (50 * (ages[4] / 100.0)); cout<<"The total price is equal to " << sum << endl; return 0; }
18th Mar 2021, 2:04 AM
Mario Iglesias
Актуальное сегодня
Help me!!