Thursday, July 30, 2009

Write a C++ program to show your skills?

Suppose you have a group of people who need to be transported on buses and vans. You can only charter a bus if you can fill it. Each bus holds 50 people. You must provide vans for 49 or fewer people who will be leftover 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 leftover that must be placed in vans. HINT: Use the modulus operator to determine the number of people leftover.

Write a C++ program to show your skills?
Don't have time to debug it, but it's pretty easy. No guarantees I'll get it exactly right.





#include %26lt;iostream%26gt;





int main(int argc, char *argv[])


{


cout %26lt;%26lt; "Enter the number of passengers: ";


int numpass;


cin %26gt;%26gt; numpass;


cout %26lt;%26lt; "You will need ";


cout %26lt;%26lt; numpass / 50; // # of buses


cout %26lt;%26lt; " buses, and ";


cout %26lt;%26lt; numpass % 50; // # of passengers left over, % is mod


cout %26lt;%26lt; " will have to be placed in vans." %26lt;%26lt; endl;


return (0);


}





Note that because in C++ division (/) of two integers always rounds down, numpass / 50 will be an integer, like you want.





For example: in C++, 425 / 50 = 8 and 425 % 50 = 25.
Reply:i havent done c++ yet, so please forgive me for not knowing the proper syntax, ill leave that up to you :)





declair/create a couple variables, one to accept user input, one for number of buses, and one for number of people in the van.





first, find out the remainder of people who need to be placed in vans.





vans = userinput %(thats modulus)50


now use that number to find out the number of buses without getting fractions


numberbuses = (userinput - vans)/50





and then display vans and numberbuses, or wahtever you are required to do with it.
Reply:cout%26gt;%26gt;"Enter the Total number of people.";


cin%26lt;%26lt;Total;





int buses = Total / 50;


int noInVan = Total % 50;





cout %26gt;%26gt; "Total number of busses req: " ;


cout %26gt;%26gt; buses;


cout %26gt;%26gt; "Total number of people in Van";


cout %26gt;%26gt; noInVan;


No comments:

Post a Comment