Monday, September 16, 2013

My Flow Chart: Minimum Coins Challenge

A question I had on Friday was, "Why would we ever need a program to tell us how many coins to give someone when we know how to easily count coins?"  It's a fair question and I will answer it after a brief description:

When I was a kid a cashier would ring up all of the items at the grocery store, people would pay the cashier (in cash), and the cashier would give currency (paper bills) to the customer.  While the cashier was counting the currency out, coins would automatically drop into a little tray in front of the customer.  Those coins were the minimum number of coins needed to avoid unnecessary trips to the bank for extra coins.  Today we don't use cash very often, so those little trays take up valuable counter space -- a space which today is typically filled with a credit/debit card reader.

However, most vending machines are still based on cash purchases [although many are slowly moving to credit/debit cards].  We typically associate vending machines with candy, snacks, and drinks.  We may put in $2.00 and buy a bottle of water for $1.25 and the machine kicks out three quarters in change.  We put in another dollar and buy a bag of chips for 80 cents and the machine kicks out 2 dimes.  You may be in a hospital waiting room and see a food machine that has sandwiches, burritos, etc.  You put in $2.00 for a $1.60 burrito and you get back a quarter, a dime and a nickel.  In some areas [amusement parks, for example] you can buy things like cameras, batteries, SD cards, t-shirts, and more.  That is all programming and [again] it's designed to kick back the minimum number of coins [or bills] for change.  When a particular coin is empty [i.e. there are no more quarters] the machine will stop accepting new money -- which means the owner of the machine will stop making money.  You COULD build in an algorithm that would say, "Uh oh... we're out of quarters.  Give two dimes and a nickel instead."  It's all up to the programmers.

Here's an example of a flow chart I made with a free online chart creator called "LucidChart".

I added process to remind me to import the Scanner (to get input) and to declare my variables.

Next I displayed (output) the Title and Instructions (asking for input in cents... 1 to 99).

Then I checked to make sure the input was in cents by less than one (i.e. .85) or greater than one dollar [100] (i.e. 154).

I added another check as an example to see if it was more than 1 but less than 2 (i.e. 1.54), although that won't happen because we will be declaring "amount" as an integer.

Next I calculate how many quarters are in the amount by dividing amount by 25.  I set the variable "quarters" to that number.

Then I figure out the remainder (the change left after quarters) by setting the new amount to amount - (quarters * 25).

I repeat these last two steps until I end up at pennies; at which time the left over amount will be less than 5 cents so whatever is left over will be in pennies.

Finally I display the coins.  Note that you COULD set up a routine that only displays a coin if there are coins in that category.  For example: "If quarters > 1 go ahead and display the quarter row."

I ended with a Stop, but we could easily have looped it back to the top to start over.

Work today to build this code, create a title graphic, and enhance your code in some way.  When you have finished, sign up for an account at Lucidchart.

No comments:

Post a Comment