- 3

There goes the question in the description.

You are making a program for a bus service. A bus can transport 50 passengers at once. Given the number of passengers waiting in the bus station as input, you need to calculate and output how many empty seats the last bus will have. Sample Input: 126 Sample Output: 24 Explanation: The first bus will transport 50 passengers, leaving 126-50=76 in the station. The next one will leave 26 in the station, thus, the last bus will take all of the 26 passengers, having 50-26=24 seats left empty. Hint: The modulo operator % can help to determine the number of passengers for the last bus.

6th Jan 2022, 5:42 AM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
7 Réponses
+ 1
Nice one Alvin✓ by making the algorithm do the modulo first (because BODMAS order of mathematical operations), then it subtracts from the bus capacity. I find when I'm trying to follow some code, if I use numbers instead of variables it can make it less complicated. What if you had: emptySeats = bus - totalPeople % bus = 50 - (126 % 50) = 50 - (26) = 24 Does the rest of the code make sense to you?
6th Jan 2022, 10:48 PM
HungryTradie
HungryTradie - avatar
+ 2
int totalPeople; cin >> totalPeople; int bus = 50; int emptySeats = bus - totalPeople % bus; cout << emptySeats; Please I need explanations on the code It's correct but I don't understand how it works Jeff.C
6th Jan 2022, 10:34 PM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
+ 1
What have you tried? Do you have some code you could add here? Did you search for other questions that may already have your answer??? https://www.sololearn.com/Discuss/2742422/?ref=app https://www.sololearn.com/Discuss/2598488/?ref=app
6th Jan 2022, 11:51 AM
HungryTradie
HungryTradie - avatar
+ 1
Tu Cent the solution should print the number of empty seats in the last bus, not the number of passengers. Also, since this is a Code Coach task, having extra text in the output is counted as wrong. Edit: one more thing... the tag says C++, not C#.
6th Jan 2022, 10:28 PM
Brian
Brian - avatar
+ 1
Good job! There is one more advanced step that some have considered, though Code Coach fails to test for it. What if the last bus is exactly full? Then emptySeats will be 50, which is wrong. It should be 0. To correct emptySeats you need one more modulo. int emptySeats = (bus - totalPeople % bus) % bus; That's how the professionals do things. Determine unspoken requirements and solve the whole problem.
6th Jan 2022, 11:46 PM
Brian
Brian - avatar
+ 1
Hmmm Let's not dive into that 😨😧😧
6th Jan 2022, 11:48 PM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
0
G'dayTu Cent that is great. The question wants the number of unoccupied seats on the last bus. Could you have one more step (or another mathematical operation to your argument of the console.writeline)
6th Jan 2022, 10:28 PM
HungryTradie
HungryTradie - avatar