CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2010
    Posts
    2

    Change Maker Project...

    I need help making a project in C++ that makes change out of a given amount (ex. $1.37) the most efficient way (4 quarters, 1 dime, 0 nickels, 2 pennies) by using the least amount of physical coins to achieve the total. The question is, what equation would I use to make this project? I have no clue where to start, and do not know the actual math behind making change. Does anyone have any samples of code or knows how to do this? Any help is greatly appreciated.

  2. #2
    Join Date
    Nov 2010
    Location
    CA
    Posts
    29

    Re: Change Maker Project...

    Quote Originally Posted by cwanime123 View Post
    I need help making a project in C++ that makes change out of a given amount (ex. $1.37) the most efficient way (4 quarters, 1 dime, 0 nickels, 2 pennies) by using the least amount of physical coins to achieve the total. The question is, what equation would I use to make this project? I have no clue where to start, and do not know the actual math behind making change. Does anyone have any samples of code or knows how to do this? Any help is greatly appreciated.
    How many quarters total?
    1.37 / 0.25 = 5.48
    5.48 throw away the remainder = 5
    Subtract total quarter value
    1.37 - (0.25 * 5) = 0.12
    This leaves 12 cents

    How many dimes?
    0.12 / 0.1 = 1.2
    1.2 throw away remainder = 1
    Subtract total dimes value
    0.12 - (0.1 * 1) = 0.02

    How many nickels?
    0.02 / 0.05 = 0.4 throw out remainder
    Nothing else to do in this step

    Pennies now.
    0.02 / 0.01 = 2
    0.02 - (0.01 * 2) = 0

    As you can see this will give you 5 quarters, 1 dime, 0 nickels, 2 pennies.

    That's the math in detailed simple steps. Just keep going until your reach zero.
    You'll want to make sure that if you are accepting input from a user that it's a valid monetary amount.
    Last edited by jsg; November 28th, 2010 at 03:42 AM.

  3. #3
    Join Date
    Apr 2008
    Posts
    118

    Re: Change Maker Project...

    Quote Originally Posted by cwanime123 View Post
    I need help making a project in C++ that makes change out of a given amount (ex. $1.37) the most efficient way (4 quarters, 1 dime, 0 nickels, 2 pennies) by using the least amount of physical coins to achieve the total.
    Assuming you're working in US dollars, shouldn't that read 1 dollar coin, 1 quarter, 1 dime and 2 pennies?

    Accordingly, the algorithm jsg provided needs a new section at the top:

    How many dollars total?
    1.37 / 1 = 1.37
    1.37 throw away the remainder = 1
    Subtract total dollar value
    1.37 - (1 * 1) = 0.37
    This leaves 37 cents

    with the following sections adjusted accordingly.

  4. #4
    Join Date
    Nov 2010
    Location
    CA
    Posts
    29

    Re: Change Maker Project...

    Quote Originally Posted by Moschops View Post
    Assuming you're working in US dollars, shouldn't that read 1 dollar coin, 1 quarter, 1 dime and 2 pennies?
    While I don't disagree with you, the OP presented his problem with the quarter being the most significant coin.

    It sounds an awful lot like a homework question, and I bet the problem doesn't mention using dollar coins. Many teachers reward initiative, but others may be annoyed that instructions were not followed correctly.

    The OP will be a better judge of what kind of demeanor this particular instructor posses.

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Change Maker Project...

    How many cents are in a dime & nickel?
    (Not from your part of the world you see)
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Change Maker Project...

    dime = 10 cents, nickle = 5 cents

    While dollar coins are technically legal tender, most stores around me will not accept them. They also mint half dollar coins, but again, most places will not accept them. They might be legally supposed to, but they don't. Follow the homework the way it's written.

  7. #7
    Join Date
    Apr 2008
    Posts
    118

    Re: Change Maker Project...

    Quote Originally Posted by ninja9578 View Post
    Follow the homework the way it's written.
    If the homework specifies "the most efficient way", we can reasonably expect this to mean using the smallest number of coins. However, the example correct behaviour itself fails to meet this requirement. The homework is written in a self-contradictory manner and there is no option but to use one's initiative.

    If this was at work, I'd kick it back to the customer with a question on it and charge them a half day

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured