Thread: Need a little help

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    33

    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.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    This is somewhat similar to questions that ask you to make change...

    So anyway, what are you plans? Any ideas?

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    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.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Oh ok. How would I incorporate the modulus operator in the code? I'm supposed to use it.

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    You can use the % to get the remainder of people that have to be transported on vans.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Ok gotcha.

    I got the divide my amount by 50, but can you elaborate on what you mean by extract the fraction from the float/double.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

  8. #8
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Here's how you find out how many people are left over.
    Code:
    int nLeftOverPeople = int(float(AMOUNT) % 50.0);

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    fmod() is better at it.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Sembhi View Post
    You must provide cans for the 49 or fewer people who will be left over after you charter buses.
    Their consolation prize is a can? That's harsh.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by citizen View Post
    fmod() is better at it.
    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.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed