+ 1

C++ Bus service question, my code works on the code playground, but not on the question itself.

#include <iostream> using namespace std; int main() { //your code goes here int totalPeople; cin >> totalPeople; int bus = 50; int trip1 = totalPeople - bus; int trip2 = totalPeople - bus * 2; int empty = bus % trip2; cout <<"the answer is.." << endl << trip1 << " after the first trip" << endl << trip2 << " after the second trip" << endl << empty << " total seats left empty on final trip" ; return 0; }

21st Nov 2020, 11:13 PM
Matt James Roper
Matt James Roper - avatar
4 Respuestas
+ 5
Matt James Roper Your program works only for 2 trips, i.e. only if the input is at most 150. If you need to subtract 50 into a loop, or to use the modulo % operator Also what is supposed to do the line with int empty?
22nd Nov 2020, 12:17 AM
Davide
Davide - avatar
+ 2
int totalPeople; cin >> totalPeople; int bus = 50; int emptySeats = bus - totalPeople % bus; cout << emptySeats;
21st Nov 2020, 11:57 PM
rodwynnejones
rodwynnejones - avatar
0
Davide the bus has 50 seats, and there are 126 people at the bus station, int empty was supposed to show how many empty seats there are on the third and final bus trip by using the modulo % operator
22nd Nov 2020, 10:49 AM
Matt James Roper
Matt James Roper - avatar
0
Matt James Roper if you take 50 from 126 two times you are left with 26 people. The empty seats are just 50-26, no need for modulo here. You need to use the modulo before, to subtract 50 multiple times. 126 was the example but in the testd there can be any number. The modulo % operator do exactly what you need to do. https://en.wikipedia.org/wiki/Modulo_operation?wprov=sfla1
22nd Nov 2020, 11:19 AM
Davide
Davide - avatar