+ 2

hi coders please can you help me to find the problem in solution this in c++

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. #include <iostream> using namespace std; 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. #include<iostream> using namespace std; int main() { //your code goes here int x,y,z; cin>>x; y=x%50; z=50%y; cout<<z; return 0; }

6th Mar 2022, 1:40 AM
Soneta
Soneta - avatar
12 odpowiedzi
0
modulo gives remainder.. so in ur statement z = 50%y gives remainder. its like 50%7 = 1. But what we need ? we need empty seats available.. so simply do z= 50-y; Thats all Thank you.. For more explanation u can text me 🥳
7th Mar 2022, 10:21 PM
Pritish Kumar Behera
Pritish Kumar Behera - avatar
+ 2
Manav Roy While that is a solution. It is not the solution. The intended solution should make use of the modulo operator. Using the modulo operator, there is a much simpler solution. The OP's original code is much closer to a better solution than the one provided in your link. If the OP had read the explanation and yellow box given for the task, the answer to correct their code would practically be shown to them. Of course, if they read the code provided well enough, they may also be able to figure out what very small change to their original code needs to be made to make it work.
6th Mar 2022, 7:21 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
ChaoticDawg & Manav Roy Thank you very much I found the solution with the (%)
7th Mar 2022, 1:19 AM
Soneta
Soneta - avatar
0
Re-read the explanation in the description of the module project and you may find where your issue is. You're close, Hint: z=50%y; this doesn't make sense to get the left over seats on the bus out of 50 when you put y passengers on that bus.
6th Mar 2022, 2:05 AM
ChaoticDawg
ChaoticDawg - avatar
0
Manav Roy Yes, your solution does work, but the whole point of that particular challenge was the use of the modulo operator. Using a loop thus defeats the purpose. Also, proper use of the modulo operator creates a very simple answer. 50 - (x % 50) <--- this simple expression can replace your whole program to give the number of remaining seats.
7th Mar 2022, 7:52 AM
ChaoticDawg
ChaoticDawg - avatar
0
Manav Roy 1st end of module project in the C++ course called Transportation.
7th Mar 2022, 7:57 AM
ChaoticDawg
ChaoticDawg - avatar
0
I will give you code
7th Mar 2022, 7:57 AM
Swar2510
0
#include <iostream> using namespace std; int main() { //your code goes here int a, bus = 50, c; cin >> a; c = bus - (a % bus); cout << c; return 0; }
7th Mar 2022, 7:58 PM
Mihai Niculae