I'm currently pretty new to programming, and I'm needing some assistance with a specific problem.
I'm supposed to make a program that takes an amount paid, and an amount owed, then yields the change in the most efficient use of dollars, quarters, dimes, nickels, and pennies. fmod would fit this perfectly, but my issue is the professor doesn't want us using anything that hasn't been discussed in class yet.
I'm positive modulus would have to play a role in this, but given that modulus can only work with integers, how could I go about making this work?
I'll exclude the code, simply due to the fact that I know why it isn't working and I understand my calculations are going to have to be rewritten.
The exact instructions are as follows:
"A third-grade teacher at Plano Elementary School wants a program that allows a student to enter the amount of money a customer owes, then an amount of money the customer has paid. The program should calculate the amount of change, as well as how many dollars, quarters, dimes, nickels, and pennies to give back to the customer. Display an appropriate message if the amount paid is less than the amount owed."
The last part is easy, but I'm really not sure how to get the exact change needed since we're using double values for currency, and can't use fmod. :(
What haven't you discussed? Structures? You should still be able to use the exact same algorithm anywhere though.
1 2 3 4 5 6 7 8 9
int quarter = cents / 25; // This give you a whole number for the number of quarters.
cents = cents % 25; // This gives us the remainder (or the cents left over)
int dime = cents / 10; // how many times can we use
cents = cents % 10; // And this is the remainder
// repeat.
I went with this code, which I actually think is an adaptation of what you wrote Stew.
I apologize, as I said, I'm really new, and for some reason the "change GetChange(int cents)" line and "struct change" messed with my head. I thought they were some reserved word for something we weren't taught yet.