Difference between revisions of "W1176 Making Change"

From Coder Merlin
Line 82: Line 82:
== Exercises ==
== Exercises ==
{{Exercises|
{{Exercises|
* Complete  Merlin Mission Manager  Mission M1177-10
* Complete  Merlin Mission Manager  Mission M1174-10
** C101 Denominations
** C101 Denominations
** C102 Change
** C102 Change
}}
}}

Revision as of 21:24, 27 January 2021

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.

String Methods[edit]

Strings are a collection of characters, each with an index that ranges from the first letter to the last letter. Using these indexes we can remove a character from a String.

We can use String Methods to perform these actions. We can find documentation on all the possible String Methods to try and use here:

Swift String Documentation

Look for a method that will allow you to remove the '$' that is at the first index of 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