-
November 28th, 2010, 12:00 AM
#1
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.
-
November 28th, 2010, 01:33 AM
#2
Re: Change Maker Project...
Originally Posted by cwanime123
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.
-
November 28th, 2010, 09:29 AM
#3
Re: Change Maker Project...
Originally Posted by cwanime123
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.
-
November 29th, 2010, 11:36 PM
#4
Re: Change Maker Project...
Originally Posted by Moschops
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.
-
November 30th, 2010, 07:10 AM
#5
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
-
November 30th, 2010, 10:02 AM
#6
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.
-
November 30th, 2010, 11:31 AM
#7
Re: Change Maker Project...
Originally Posted by ninja9578
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|