Beginning Programming (Pascal) Lecture 17 COMSC 1513 Making ...

5 downloads 68 Views 12KB Size Report
COMSC 1513. Making change. 1 ... 337>=100 Deliver a $100-bill and decrement 337 by 100 yielding 237. 3.2. ... 37
Beginning Programming (Pascal) COMSC 1513

Lecture 17 Making change

Consider making change given a dollar amount. The algorithm is simple: 1. Start with an amount for which change is desired. 2. Start with the bill having largest denomination 3. For a particular denomination 3.1. While the remaining amount greater than or equal to the denomination 3.1.1. Deliver a bill having the given denomination 3.1.2. Decrement the remaining amount by the value of the denomination 3.2. Go on to the next smaller denomination. Example: Make change for $4837 1. Denomination=$1000 1.1. 4837>=1000  Deliver a $1000-bill and decrement 4837 by 1000 yielding 1.2. 3837>=1000  Deliver a $1000-bill and decrement 3837 by 1000 yielding 1.3. 2837>=1000  Deliver a $1000-bill and decrement 2837 by 1000 yielding 1.4. 1837>=1000  Deliver a $1000-bill and decrement 1837 by 1000 yielding 1.5. 837=500  Deliver a $500-bill and decrement 837 by 500 yielding 337 2.2. 337=100  Deliver a $100-bill and decrement 337 by 100 yielding 237 3.2. 237>=100  Deliver a $100-bill and decrement 237 by 100 yielding 137 3.3. 137>=100  Deliver a $100-bill and decrement 137 by 100 yielding 37 3.4. 37