+ 1

question about Ticket office test, C++

this is my first post here. so i can't say if this is the correct format. i made this post because i was wondering if there were better ways of doing this test, then how i completed it, because i wasn't really satisfied with how i did it. and also because i couldn't really see any way to implement any of the stuff beyond what was taught in the basic modules. test "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" #include <iostream> using namespace std; int main() { int ages[5]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; } //insert your code here int min; float discount = 150; for(int a = 0; a < 5; ++a) { min = ages[a]; if (min < discount) { discount = min; } } float price = 50 - 50 * (discount * 0.01); cout << price << endl; //end of your code return 0; }

22nd Jun 2021, 9:29 PM
AndXr
3 Answers
+ 1
Although your program works as intended, here are some things you can do to make it function a bit more efficiently :- 1) you can calculate the minimum while taking the input itself ( saving your program to iterate over array again and decreasing the overall time taken by your program ) 2) if you look closely, then you can see that the discount is only decided by the person with smallest age and the cost of the ticket is fixed irrespective of other ages, so it is not mandatory to store all the ages in an array and is better to just keep track of the smallest age instead ( will decrease the overall space consumed by your program ). Here's how I would have implemented it, just from the stuff learned up till that module.👇 https://code.sololearn.com/cco8hTMjbyfR/?ref=app
23rd Jun 2021, 12:58 AM
Arsenic
Arsenic - avatar
+ 1
i'm a little bit confused. could you give examples? and I only wrote the section between "//insert your code", and "//end of your code." as i'm not sure if you are supposed to edit whats actually provided or not. so with 1. wouldn't that require editing the loop they already made? i might be miss-understanding you. 2. i didn't store anything within the array. that was already there as part of the test. i just tried to get the values from the array, to see which was the smallest, to figure out the discount price. although i probably should have used different variable names lol. those were what came to mind. either way. what i was looking for, was a way of implementing some of the other lessons, beyond just the variables, if's and loops (which is what i'm used to using from modding). I feel like there's alot of really cool stuff in the lessons. but i have no idea how to use any of it, except for the basics i've been working with for years. But. if you can like show me how to make it more efficient aswell. even if it's just with that stuff. i'd really appreciate that aswell. maybe use another example. i appreciate the reply. here's how it looks without my section. ============================================= #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; }
23rd Jun 2021, 4:36 PM
AndXr
0
AndXr #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 m; float p; m = ages[0]; for (int x = 1; x < 5; ++x) { if ( m > ages[x]) { m = ages[x]; } } p = 50 - (50*m/100); cout << p << endl; return 0; }
1st Jan 2022, 11:12 AM
shlee_
shlee_ - avatar