+ 3

Transportation project help

There is a project for a transportation of buses to calculate how many seats are left here in SoloLearn using C++, I can’t seem to understand how would this be solved.

21st Nov 2020, 11:29 PM
Jonathan E
Jonathan E - avatar
10 Answers
+ 4
A simple three lines of code implementation. First, you use the modulus function to find the remainder, which are the passengers on the last bus. Then, to find the empty seats, you use bus capacity minus away the last passengers. https://code.sololearn.com/cAsIKRZH3quK/?ref=app
16th Dec 2020, 12:37 PM
Lam Wei Li
Lam Wei Li - avatar
+ 2
Have you come across the modulo "%" operator? You'd need to use it for this problem.
21st Nov 2020, 11:44 PM
rodwynnejones
rodwynnejones - avatar
+ 2
examples are provided for clarification sololearn checks your code with 5 or 6 different inputs (with different data to test various possibilities) essentially your code will run 5 or 6 times if your code is good enough and the output matches their expected input you win the challange. here is code with a few comments to explain what each line does. int waiting = 0; // define variable int seats =0; cin >> waiting; //get total waiting as an input value seats = waiting % 50; //mod operator will return seats filled in last bus cout << 50 - seats ; //total seats(50) - occupied seats is the expected answer
26th Jan 2021, 5:21 AM
LKScoder
LKScoder - avatar
+ 1
busCapacity - NoOfpeople % busCapacity.
22nd Nov 2020, 8:41 AM
rodwynnejones
rodwynnejones - avatar
+ 1
There are multiple ways of doing this, the simplest way I can think of is to have 3 integers, 1 for last bus passengers, 1 for user input, and the last one for the seats remaining. so what you would do is something along the lines of : int n; // this will be one for user input int lastBus; // the passengers on the last bus, this is used to calculate the seats remaining int seatsRemaining; // this will be your answer cin >> n; // this will take the user input lastBus = n%50; // this uses modulus operator to figure out the amount of passengers on the last bus seatsRemaining = 50-lastBus; // this takes bus capacity which is 50 - the passengers on the last bus to calculate open seats cout << seatsRemaining; // this gives your output that sololearn will check There are other ways of doing this but that will use things you will only learn later on, the above code only uses what you should have learned so far. Post might be late but I hope it will help others that come on a later date.
20th Mar 2021, 7:09 PM
Johan Van Loggerenberg
Johan Van Loggerenberg - avatar
0
yeah but i cant make sense of it
22nd Nov 2020, 3:02 AM
Jonathan E
Jonathan E - avatar
0
this is my code but its asking for both outputs of 38 and 19 seperately #include <iostream> using namespace std; int main() { int x; int bus; int res; bus = 50; //res = bus - 12; //cout << res << endl; x = 50 * 4; x = 231 - x; x = bus % x; cout << x << endl; return 0; }
27th Nov 2020, 12:56 AM
Jonathan E
Jonathan E - avatar
0
oh wow i did not think of that
10th Dec 2020, 5:42 AM
Jonathan E
Jonathan E - avatar
0
Im so lost here.
12th Apr 2021, 2:03 PM
J A M E S
J A M E S - avatar
- 1
int totalNumPass = 0; int maxBusSeats = 50; int emptySeats = 0; cin>> totalNumPass; if ( totalNumPass > maxBusSeats ) { while (totalNumPass > maxBusSeats) { totalNumPass -= maxBusSeats; } emptySeats = maxBusSeats % totalNumPass; cout << emptySeats; } else if ( totalNumPass < maxBusSeats ) { emptySeats = maxBusSeats - totalNumPass; cout << emptySeats; } else if( totalNumPass = maxBusSeats) { cout << 0; } else if ( totalNumPass < 0) { cout << "Error"; }
20th Aug 2021, 10:05 PM
Othman Djuliarso
Othman Djuliarso - avatar