Cash drawer 🌶️🌶️🌶️
A cash drawer has to make change. cents holds an amount of money in cents (41 means 41 cents). Make that amount using as few coins as possible.
In the box, declare:
- four
intvariables,quarters,dimes,nickelsandpennies, holding how many of each coin are needed (a quarter is 25 cents, a dime 10, a nickel 5, a penny 1); - a
doublenamedaverageCoinValue: the ORIGINAL number of cents divided by the total number of coins.
The five locked lines at the bottom print those five variables, one per line. For example, 41 cents is 1 quarter, 1 dime, 1 nickel and 1 penny: 4 coins, so the average coin is worth 10.25 cents.
Use
/ to count whole coins and % to find what is left over. Watch out for two things: what happens to the amount as you use up coins, and where the decimal part of the average comes from. Your code will be tested with several different amounts.
Start with the biggest coin. cents / 25 is the number of whole quarters (int division drops the remainder). What amount is left after you take those quarters out? That is exactly what cents % 25 gives you.
Does each smaller coin work on the original amount, or on what's left after the bigger coins? Do you still need the original amount later?
Does int division give you a decimal answer? When does the cast need to happen — before or after the division?