Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 22nd, 2003, 12:06 PM
 Gustavo
Contributing User

Join Date: Sep 2003
Posts: 36
Time spent in forums: < 1 sec
Reputation Power: 11
Make Change

I am to write a program in C++ that will take a dollar and cents amount, and break it down and tell how many dollars, halves, quarters, dimes, nickels, and pennies are used in that amount. Now I ask for some help on this. This is a program I am to write, so of course I do not want everything done for me. But I would like a few hints and whatnot here and there. I would very much appreciate that. Thank you.

#2
September 22nd, 2003, 02:18 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,534
Time spent in forums: 2 Months 3 Days 4 h 53 m 19 sec
Reputation Power: 4106
You probably want to use the / and % operators here. Note that when using int variables, the / operator performs integer division (and thus rounds down automatically). This is how I would do it.

1. Input a value from the user as dollars and cents into a float variable.
2. Convert the dollars and cents to cents alone and assign it to an int variable.
3. Divide (i.e. use /) the int variable by 100 to get the number of dollars to use.
4. Modulo divide (i.e. use %) the int variable by 100 to get the remainder.
5. Divide the number from step 4 by 50 to get the number of half-dollars to use
6. Modulo divide the result from step 4 by 50 to get the remainder
7. Divide the number from step 6 by 25 to get the number of quarters
8. Modulo divide the result from step 6 by 25 to get the remainder
9. Continue the same steps for dimes, nickels etc.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

#3
September 22nd, 2003, 02:26 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,841
Time spent in forums: 3 Months 22 h 23 m 49 sec
Reputation Power: 2199
My mother worked for the post office before she married. Since they didn't have anything to calculate the change for them, all clerks had learned a simple and reliable way of giving change that the customer could also follow very easily. And she taught it to me. And now I will teach it to you.

2. Count out loud up to the amount the customer paid with, skipping those steps that do not apply:
a. With pennies, count up to the nearest nickel (ie, to the amount of cents evenly divisible by five).
b. With nickels, count up to the nearest dime.
c. With dimes, count up to the nearest quarter.
d. With quarters, count up to the nearest dollar.
e. With dollars, count up to the nearest five dollars.
f. And so on until you arrive at the amount paid.

Of course, steps 2b and 2c could be reversed, depending on which will get you to that quarter. And if you don't have enough of a particular coin, then you would need to substitute others. But the basic procedure is there and works very well.

Example:
The bill is \$3.52 and you pay with twenty-dollar bill --
The bill: "\$3.52"
With pennies: "53, 54, 55" 3 pennies
To get to the next quarter will take dimes, so we skip the nickels.
With dimes: "65, 75" 2 dimes
With quarters: "\$4.00" 1 quarter
With ones: "\$5.00" 1 one
With fives: "\$10.00" 1 five
With tens: "\$20.00" 1 ten
Change was \$16.48 in 1 ten, 1 five, 1 one, 1 quarter, 2 dimes, and 3 pennies.

Giving change used to be a basic skill that every cashier needed to know, but now it's a lost art. I've been in a store (ironically in a computer store) when the power when out and it would have been hilarious to watch the clerks struggle to make change if it weren't wasting so much time. They were even having trouble figuring out the sales tax with hand-held calculators; I guess they no longer have a tax table to look it up on.

Last edited by dwise1_aol : September 22nd, 2003 at 02:31 PM.

#4
September 22nd, 2003, 10:54 PM
 damonbrinkley
Contributing User

Join Date: Aug 2002
Location: NC, USA
Posts: 364
Time spent in forums: 26 m 21 sec
Reputation Power: 12
Here's a coin calculator that I wrote in Python. You can use the same logic as I did.

Code:
```#!/usr/bin/env python

coins = int(raw_input("Enter an amount less than \$1.00: "))
print coins, "coins"

#quarters
quarters = coins / 25
print quarters, "quarters"
coins = coins % 25

#dimes
if coins / 10 < 1:
dimes = 0
else:
dimes = coins / 10
print dimes, "dimes"
coins = coins % 10

#nickels
if coins / 5 < 1:
nickels = 0
else:
nickels = coins / 10
print nickels, "nickels"
coins = coins % 5

#pennies
pennies = coins / 1
print pennies, "pennies"

```

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Make Change