Project: Transportation (C++) | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 7
Try it in this way : #include <iostream> using namespace std; int main() { //your code goes here int people,seats=50,empty=0; cin>>people ; if(people >50) { empty =people%seats; seats =seats-empty; } else { seats=seats-people; } cout <<seats; return 0; }
20th Feb 2021, 10:56 PM
Aysha
Aysha - avatar
+ 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