Need a little help
Could someone please give me an idea on how to do this?
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 cans 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 deterimins how many buses must be chartered and reports the number of people left over that must be placed on bans.
Also each van can hold 15 people. How many vans will be required?
Not looking for you guys to do it or anything...just need some help on where to start.
This is somewhat similar to questions that ask you to make change...
So anyway, what are you plans? Any ideas?
First, divide the amount by your 50, then extract the fraction from the float/double.
The whole left over is the amount of buses. Do the same with the extraction.
I could write the whole program for you in less than 5 minutes, but this does sound like homework to me.
Oh ok. How would I incorporate the modulus operator in the code? I'm supposed to use it.
You can use the % to get the remainder of people that have to be transported on vans.
I got the divide my amount by 50, but can you elaborate on what you mean by extract the fraction from the float/double.
Actually I think that extracting a fraction is a big mistake. Use some integer math, because dealing with parts of people will only make your results fuzzy.
I don't see why you can't simply
1. Divide the group by the number of bus seats to find the number of buses.
2. Take away the people on buses from the group to find the leftovers.
3. Divide the leftovers by the number of seats in a van to find the number of vans.
In my solution, it turns out that 145 people will need 2 buses and 3 vans to go on vacation, and I made sure it added up.
Here's how you find out how many people are left over.
int nLeftOverPeople = int(float(AMOUNT) % 50.0);
Their consolation prize is a can? That's harsh.
Originally Posted by Sembhi
I agree with that, but there's absolutely no reason to use floating point in this case, as citizen pointed out before. It just a possible cause for strange errors as the results are not precise in floating point, and you can end up with results caused by rounding down.
Originally Posted by citizen