Hi All,
As a non-required, ungraded, "activity", I coded the following program, and I'm wondering if there was an easier way for me to of solved the problem. Please note that this is only a second semester type course, and to this point we've only covered basic variables and constants, as well as the order of operations - we've yet to tackle strings, arrays and the like - so, I don't need to be fancy, but any pointers towards more efficient coding practices would be greatly appreaciated! :)
// File Name: transport.cpp
// Activity 3-1
// Author: (aeinstein)
// Date: 05.28.2006
//Activity 3-1:
// "Suppose you have a group of people that needs to be transported on buses and vans.
// You can charter a bus only if you can fill it. Each bus holds 50 people. You must
// provide vans for the 49 or fewer people who will be left over after you charter buses.
// Write a program that accepts a number of people and determines how many buses must be
// chartered and reports the number of people left over that must be placed on vans.
// Hint: Use the modulus operator to determine the number of people left over."
#include <iostream.h> // necessary for cout command
int main()
{
short buses, passengers, van; // declare buses, passengers & van as short
buses = passengers = van = 0; // initialize buses, passengers & van to 0 (not neccessary, but...)
int cutoff; // declare cutoff as int
cutoff = 50; // initialize cutoff to 50
cout << "How many people in your group need transportation? "; // print prompt for user input to screen
cin >> passengers; // assign user input value to the variable passengers
buses = passengers / cutoff; //calculate number of buses needed via passengers/cutoff
van = passengers % cutoff; // calculate number of people needing to be "van'd" via passengers%cutoff
cout << "With " << passengers << " people requiring transportation, "; // print user advisory line 1 of 2, as an input confirmation, to screen
cout << buses << " buses will be needed and " << van; // print user advisory line 2 of 2 to screen,
cout << " passengers will need to go by van.\n"; // which I coded (line 2 of 2) as two code statements to compact commenting.
return 0;
}
I realize I could have coded the last two code statements (preceeding "return 0;") as one statement via: cout << buses << " buses will be needed and << van << " passengers will need to go by van.\n";
, but I like to align all of my comments five spaces to the right of my longest code statement, so I broke it up as quoted up above. I'm not sure how practical that practice will be moving forward, or in the "real world", but it seems like an acceptable practice to me for now (any suggestions on that as well?).
Regards,
aeinstein