+ 1

Project: Transportation (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. 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. // 1. I don’t understand how ‘cin >> 126’ could result in ‘cout << 24’ in this scenario. // 2. I don’t understand why my coding failed Test Case #1 and #2, which both tell me my output is 24. // Here is my code: int a; int b; a = 26, b = 50; int difference; difference = b - a; cin >> a; cout << difference ;

20th Feb 2021, 9:31 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
15 Respuestas
+ 4
Remember, try to take input when assigning value doesnot work. You don't need 4 different values here. You can solve this way as well: #include <iostream> using namespace std; int main() { int input; cin >> input; cout << 50 - input % 50; return 0; } // modulus operator => (%) is known as the remainder operator which returns the remainder after an integer is divided. // Here while solving, you will be using modulus function to find the remainder, how many passengers are remaining/left on the bus. Then, for finding empty seats, you use bus capacity minus away the last passengers. //Hope this helps.
20th Feb 2021, 11:17 PM
Shivani 📚✍
Shivani 📚✍ - avatar
+ 4
Keep learning Tahiti ! 😂😂👍
21st Feb 2021, 2:32 AM
Shivani 📚✍
Shivani 📚✍ - avatar
+ 3
I finally completed my first project!!! 🍾 It looks like I’ll be learning about the ‘if’ function next. I think I should review the “%” once more before proceeding. Shivani 📚✍ , I used your guidance regarding use of %. I used variable “a” in place of variable “input”. It worked, and I am once again encouraged! 😁
21st Feb 2021, 12:31 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 2
You seem to be on the right track, but you only receive 1 integer as input, but you are trying to read 3.
20th Feb 2021, 10:16 PM
Dennis
Dennis - avatar
+ 2
This will help you if you like: int min = ages[0]; double total = 50; for (int i = 0; i < 5; ++i) { if(ages[i] < min) min = ages[i]; } total -= total * min / 100.0; cout << total;
20th Feb 2021, 10:59 PM
JaScript
JaScript - avatar
+ 2
Thank you, Aysha and JaScript ! I haven’t gotten that far yet to learn concepts such as coding ‘if’, ‘else’, ‘for’, or using the [ ] or multiple { }. I’m trying to understand the guidance provided, but having trouble.
21st Feb 2021, 12:00 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 2
So here is how I understand your fourth line: b=50-(a%50); //seats left empty// cout<<b<<endl; } // b is the final answer, which is # of empty seats. Calculated as 50 (total seats) minus (the remainder of 126 divided by 50). In other words, 50 minus 26. b equals 24. Thank you!
23rd Feb 2021, 4:47 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
// Second try. #include <iostream> using namespace std; int main() { int a; int b; int c; int d; a = 126, b = 76, c = 26, d = 50; int remainder; remainder = d - c; cin >> a >> b >> c; cout << remainder ; return 0; // I still don’t understand why, when I run/test the code, it tells me my output is 24, but says that it’s wrong.
20th Feb 2021, 10:01 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
// Third try. #include <iostream> using namespace std; int main() { int a; int b; int c; int d; a = 126, b = 76, c = 26, d = 50; cin >> a; cin >> b; cin >> c; cout << (d%c); return 0; }
20th Feb 2021, 10:12 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
//Fourth attempt: #include <iostream> using namespace std; int main() { int a; int b; int c; int d; a = 126, b = 76, c = 26, d = 50; cin >> a; cout << (d%c); return 0; }
20th Feb 2021, 10:16 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
Try to use this #include <iostream> using namespace std; int main() { int a; // passengers waiting// int b; //the result(output)// cin>>a; b=50-(a%50); //seats left empty// cout<<b<<endl; return 0; }
22nd Feb 2021, 6:05 AM
Marios
Marios - avatar
+ 1
Using Modulus (%) means: It shows what will remain if we divide the number to the left with the number to the right but in terms of integer only. In our example if we divide (a=126) with (50=passengers of the bus). In other words what will remain if you keep substract 50 from 126. 126-50=76 76-50=26. //Thats how you get the number of empty seats. I hope this explanation helped you...
23rd Feb 2021, 6:07 AM
Marios
Marios - avatar
0
Shivani 📚✍ , I thank you again. Do I understand your code means that the integer is defined as “input”, the user enters “26” as the input, and the screen prints the remainder of 50 after subtracting 26 (input) from 50? //Also, could “input” be substituted as any variable I decide to use? For example: int main () int text; cin >> text; cout << 50 - text % 50; ?
21st Feb 2021, 12:08 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
0
Marios Thank you. This is very helpful! Could you please explain (a%50) in layman’s terms?
23rd Feb 2021, 4:28 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar