September 22nd, 2003, 12:06 PM
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.
September 22nd, 2003, 02:18 PM
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
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
September 22nd, 2003, 02:26 PM
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.
1. Start with the amount of the purchase.
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.
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.
September 22nd, 2003, 10:54 PM
Here's a coin calculator that I wrote in Python. You can use the same logic as I did.
coins = int(raw_input("Enter an amount less than $1.00: "))
print coins, "coins"
quarters = coins / 25
print quarters, "quarters"
coins = coins % 25
if coins / 10 < 1:
dimes = 0
dimes = coins / 10
print dimes, "dimes"
coins = coins % 10
if coins / 5 < 1:
nickels = 0
nickels = coins / 10
print nickels, "nickels"
coins = coins % 5
pennies = coins / 1
print pennies, "pennies"