+ 3

C++ Ticket Office problem solved but not marked as done

So I wrote a very basic and ugly noob program to solve this problem. The issue is it doesn't seem like giving me credit for. The program completed the first 2, and the 3rd and 4th hidden Test Cases. I wonder why isn't marked as done?! as you can see here: https://i.postimg.cc/Dn46T8X7/Desktop-15-01-2021-00-48-30-380.png The Problem if someone interested in in the Data Types, Arrays, Pointer's section: Ticket Office 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]; } //your code goes here return 0; } -------------------------------------------------------------------------------------------------------- Input is given. Test case 1 : Test case 2: Rest is hidden as you can see on the link. 11 69 18 48 19 33 22 25 49 29

14th Jan 2021, 11:17 PM
fergoka
fergoka - avatar
9 Réponses
+ 6
I've re-written in a similar manner Mateusz has mentioned and some others on the forums. #include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } //your code goes here float perc; float min = ages[0]; for (int x = 1; x < 5; ++x){ if(min > ages[x]) min = ages[x]; } perc = 50-(50*min/100); cout << perc << endl; return 0; } It works perfectly and looks great. I've also got the xp just as you mentioned, Arsenic. I'd still like to find or find out new ways to do this. (Under no circumstances I'd come up with this code by myself I feel like)
15th Jan 2021, 4:00 PM
fergoka
fergoka - avatar
+ 4
In the screenshot you posted it fails on the test case 3. There can be few problems. First is the amount of code and how you search for min value. This is very inefficient and prone to errors. Better way would be to use a for loop. Something like that: int min_age = ages[0]; for(int iage=1; iage < 5; ++iage) { if (ages[iage] < min_age) { min_age = ages[iage]; } } No unnecessary code repetition and less space for mistakes. My guess is that the error comes from how you check for minimum value. Your if statement checks only if the value is smaller. What happens if the value is equal? Say you have 18, 18, 25. 18 is lesst than 25, but not less that 18, so it will evaluate to false and you will not find a minimum value.
14th Jan 2021, 11:31 PM
Mateusz Malenta
Mateusz Malenta - avatar
+ 4
I want to see what you submitted as solution of this problem fergoka P.S. the problem is not considered solved till the time it doesn't pass all the tests. check this out to know why SL hides them 👇 https://www.sololearn.com/Discuss/2662203/?ref=app
15th Jan 2021, 2:00 AM
Arsenic
Arsenic - avatar
0
So basically what happeds there if I understand is that you check for the lowest numbers in a for loop and overwrite the lowest on top the rest in the min_age variable. It's really an elegant way to solve that. I couldn't find out a better way than that terrible one I jut did :) It almost seems like your is a template type of way for checking for minimums and maximums in an array within a loop. Am I right? Thanks for the reply. Also, I don't think hidden Test Cases should be solved...until they become un-hidden at least.
15th Jan 2021, 12:31 AM
fergoka
fergoka - avatar
0
Arsenic thanks for your answer and link. It's most likely the case Mateusz has mentioned and so there are at least 2 persons of the same age, my crappy and inefficient program is unable to check. This is what I've submitted://( I re-write it today soon and share it here.) #include <iostream> using namespace std; int main() { int ages[5]; float cost = 50; // total cost for 5 persons aka 50$ int x = 0; // youngest age float per; // x age in percentage float disc; // total discount for (int i = 0; i < 5; ++i) { cin >> ages[i]; } if(ages[0] < ages[1] && ages[0] < ages[2] && ages[0] < ages[3] && ages[0] < ages[4]) {//checkin which number is the smallest in the array == youngest person in the group x = ages[0]; per = (x*cost)/100; //Rule of third for percentage calculation disc = cost - per; cout << disc << endl; } if(ages[1] < ages[0] && ages[1] < ages[2] && ages[1] < ages[3] && ages[1] < ages[4]) { x = ages[1]; per = (x*cost)/100; //Rule of third for percentage calculation disc = cost - per; cout << disc << endl; } if(ages[2] < ages[0] && ages[2] < ages[1] && ages[2] < ages[3] && ages[2] < ages[4]) { x = ages[2]; per = (x*cost)/100; //Rule of third for percentage calculation disc = cost - per; cout << disc << endl; } if(ages[3] < ages[0] && ages[3] < ages[1] && ages[3] < ages[2] && ages[3] < ages[4]) { x = ages[3]; per = (x*cost)/100; //Rule of third for percentage calculation disc = cost - per; cout << disc << endl; } if(ages[4] < ages[0] && ages[4] < ages[1] && ages[4] < ages[2] && ages[4] < ages[3]) { x = ages[4]; per = (x*cost)/100; //Rule of third for percentage calculation disc = cost - per; cout << disc << endl; } return 0; }
15th Jan 2021, 3:46 PM
fergoka
fergoka - avatar
0
this is the right code : #include <iostream> using namespace std; int main() { int ages[5]; int mn=10000; for (int i = 0; i < 5; ++i) { int age; cin >> age; ages[i] = age; mn = min(mn,age); } //your code goes here float disc = float(mn)/float(100); float price = 50 * (1-disc); cout<<price<<endl; return 0; }
22nd Feb 2022, 10:56 AM
Mohd Arif
0
#include <iostream> using namespace std; int main() { float ages[5],dis,min; for (int i = 0; i < 5; ++i) {//getting values or input cin >> ages[i]; } //your code goes here min=ages[0];//initialized minimum value to array's 0th location or first element for(int i=0;i<5;i++)//getting the smallest element in array using for loop and if statement { if(ages[i]<min) min=ages[i]; } dis=(100-min)/100*50; cout<<dis; return 0; }
29th Apr 2022, 3:33 AM
RAHMAN HUSAIN
0
For loop lagadiya hota ye sab kya kar diya bro
14th Oct 2022, 2:18 AM
Dhruv Joshi
Dhruv Joshi - avatar
- 1
//HERE IS THE SOLUTION #include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } //your code goes here float temp = ages[0]; for(int i=0;i<5;i++) { if(temp>ages[i]) { temp=ages[i]; } } float d = (50)*(temp/100); float total = 50-d; cout<<total; return 0; }
20th Oct 2022, 8:13 AM
Dhruv Joshi
Dhruv Joshi - avatar