W1176 Making Change

From Coder Merlin
Revision as of 15:05, 28 January 2021 by Jonathan-he (talk | contribs)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Monnaie de Bactriane, Eucratide I, pile

Prerequisites[edit]

Introduction[edit]

When paying in cash for a product at a store, it is very unlikely you have the exact amount of money in bills and coins needed to pay. Usually you pay some amount over, and have the result of the (paid - total) returned back to us as change.

When giving change you always want to give the largest denomination of bills and coins back to the customer so you do not burden them with extra smaller bills and coins.

Denominations[edit]

For making change in this exercise, the available denominations are limited to:

    Bills
    $100 bill
    $50 bill
    $20 bill
    $10 bill
    $5 bill
    $2 bill
    $1 bill
    Coins
    $1 (one dollar coin)
    $0.50 (half-dollar coin)
    $0.25 (quarter)
    $0.10 (dime)
    $0.05 (nickel)
    $0.01 (penny)


Calculating Change[edit]

When trying to think of an algorithm, approach the problem as you would with money in front of you. Think about how you can check if your bill will fit in the amount of many specified. Usually you follow some steps similar to these.

    Check the next largest bill.
    Does it fit in the total?
    If yes, how many times?
    Store the amount of times.
    Subtract the bills you just found from the total so you can try the next size.


Hint.pngHelpful Hint

You will be doing these same steps to check every bill and coin value. Using a Function would be helpful since you are repeating code over and over again.

String to Double[edit]

All of our money values are given to us as Strings formatted with a dollar sign.

"$123.45"

In order to calculate bills and coins that can fit into this amount, we need to Cast our amount from a String to a Double.

Try:

Double("$123.45")!

So what happened? Since we have the dollar sign in our string, we cannot convert our String into a Double. We need a way to work around this.

We want to either build an algorithm that can go through each digit to check if it is a number, and built an integer that we can use, or use a method to remove the dollar sign from the string.


Key Concepts[edit]

Key ConceptsKeyConceptsIcon.png
  • Use a function to reuse code, to keep yourself from rewriting your algorithm over and over
  • Using String Methods to remove characters so you can cast a String to a Double

Exercises[edit]

ExercisesExercisesIcon.png
  • Complete Merlin Mission Manager Mission M1174-10
    • C101 Denominations
    • C102 Change