+ 1
I need help here. Tried severally but failed to produce the desired output
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.
7 Respostas
+ 4
I see you know how to add c++ codes to Sololearn's Code Playground and publish them. Publish your attempt and share a link here. Don't be shy about publishing buggy code. People do it all the time.
Posting the question description doesn't show me what you tried. I could share my code to solve it but that won't help any.
+ 2
Use the modulo operator on the input amount with the number of seats on a bus to get the remaining passengers then subtract the amount of remaining passengers from the number of seats on a bus to get the the number of seats left empty and output that number.
Pseudocode:
seatsPerBus = 50
emptySeats = seatsPerBuss - (inputNum % seatsPerBus)
output emptySeats
+ 1
To pass the end of module project, you need to change your code so that totalP isn't a hard coded value of 126, but is received as an input (cin, fgets(), etc).
Then you need to output only the value of emptySeats without any other characters.
cout << emptySeats;
Also, you should have;
return 0;
as the last statement of your main() function.
+ 1
You need the EXACT output they want to pass the tests so the extra "emptySeats = " will fail.
You also need to read the input from standard input. Unless 126 is the only input given in all test cases, some cases will fail.
Something like this should work. You have a pretty close attempt so I think it is fine to share working code right away instead of just describing the changes:
#include <iostream>
using namespace std;
int main ()
{
int totalP;
int seatsPerBus =50;
int emptySeats;
cin >> totalP; // important change #1
emptySeats = seatsPerBus - (totalP % seatsPerBus );
cout << emptySeats; // important change #2
}
0
https://code.sololearn.com/co9gSVQIdI9S/?ref=app
Produced an error
0
Josh Greig tried your idea but still same thing. My output and their sample output was the same but they said I've failed.
0
Prince Bedtop wrote, "Josh Greig tried your idea but still same thing. My output and their sample output was the same but they said I've failed."
Response:
That's very weird.
I copy/pasted the updated code I shared with you and it passed all cases so I don't know why it would fail for you.
This is exactly what I did:
1. I pasted into the code editor at: https://www.sololearn.com/learning/eom-project/1051/902
So there is no confusion, here is another copy of the code:
#include <iostream>
using namespace std;
int main ()
{
int totalP;
int seatsPerBus =50;
int emptySeats;
cin >> totalP; // important change #1
emptySeats = seatsPerBus - (totalP % seatsPerBus );
cout << emptySeats; // important change #2
}
2. Hit "Run".
3. Then a second later, I saw all test cases passed. It said "Practice makes perfect" indicating everything was fine. It should say "Congratulations" or something else for you since you didn't finish it yet.
Leaving out the "return 0;" at the end of main can lead to a compiler warning sometimes but it didn't cause a problem for me.
I think this was my original solution which also passes:
#include <iostream>
using namespace std;
int main ()
{
int numInput;
cin >> numInput;
cout << (50 - (numInput % 50 ));
return 0;
}