Pseudocode Examples
Pseudocode Examples
Making Change
(in US Currency)
While there are many ways to express a make change
algorithm, well take an approach that illustrates how
algorithms can be reused within other algorithms
To eliminate ambiguity, we give names to items that we care about and make sure that we
use them with absolute consistency
Top-down, see?
makeChange
countQuarters
countDimes
countNickels
countPennies
procedure countQuarters(amount)
currentAmount ! amount
coinCount ! 0
while (currentAmount ! 25) do
(coinCount ! coinCount + 1
currentAmount ! currentAmount 25)
return coinCount
Avoiding Redundancy
Using countQuarters() as a pattern, it isnt too hard to figure
out the rest of the count() algorithms:
procedure countDimes(amount)
currentAmount ! amount
coinCount ! 0
while (currentAmount ! 10)
(coinCount ! coinCount + 1
currentAmount ! currentAmount 10)
return coinCount
procedure countPennies(amount)
currentAmount ! amount
coinCount ! 0
while (currentAmount ! 1)
(coinCount ! coinCount + 1
currentAmount ! currentAmount 1)
return coinCount
procedure countNickels(amount)
currentAmount ! amount
coinCount ! 0
while (currentAmount ! 5)
(coinCount ! coinCount + 1
currentAmount ! currentAmount 5)
return coinCount
procedure countDimes(amount)
currentAmount ! amount
coinCount ! 0
while (currentAmount ! 10)
(coinCount ! coinCount + 1
currentAmount ! currentAmount 10)
return coinCount
procedure countNickels(amount)
currentAmount ! amount
coinCount ! 0
while (currentAmount ! 5)
(coinCount ! coinCount + 1
currentAmount ! currentAmount 5)
return coinCount
procedure countPennies(amount)
procedure countCoins(amount, denomination)
currentAmount ! amount
currentAmount ! amount
coinCount ! 0
coinCount ! 0
while (currentAmount ! 1)
while (currentAmount ! denomination)
(coinCount ! coinCount + 1
(coinCount ! coinCount + 1
currentAmount ! currentAmount 1)
currentAmount ! currentAmount denomination)
return coinCount
return coinCount
Expanding to Other
Currencies
At this point, our algorithm looks like this:
procedure countCoins(amount, denomination)
currentAmount ! amount
coinCount ! 0
while (currentAmount ! denomination)
(coinCount ! coinCount + 1
currentAmount ! currentAmount denomination)
return coinCount
procedure makeChange(amount)
currentAmount ! amount
quarters ! countCoins(currentAmount, 25)
currentAmount ! currentAmount (25 ! quarters)
dimes ! countCoins(currentAmount, 10)
currentAmount ! currentAmount (10 ! dimes)
nickels ! countCoins(currentAmount, 5)
currentAmount ! currentAmount (5 ! nickels)
pennies ! countCoins(currentAmount, 1)
return [quarters, dimes, nickels, pennies]
The version on the left builds a list of numbers to sum up in the end (making it easier to do
by hand), while the version on the right adds the numbers up right away (making it easier to
translate for a computer)in the end, the result is the same can you see why?
Both versions use one more major tool in specifying algorithms: a conditional statement
that does one thing if some condition is true, but does something else (or nothing at all) if
that condition is false, indicated by an if then or if then else construct
procedure listRPM(factor1, factor2)
if (factor1 > factor2) then
(term1 ! factor2
term2 ! factor1)
else
(term1 ! factor1
term2 ! factor2)
addendList ! [ ]
while (term1 > 0)
(if (term1 is odd) then
(add term2 to addendList)
term1 ! halveWithoutRemainder(term1)
term2 ! double(term2))
product ! 0
while (term1 > 0)
(if (term1 is odd) then
(product ! product + term2)
term1 ! halveWithoutRemainder(term1)
term2 ! double(term2))
product ! 0
for each (number in addendList)
(product ! product + number)
return product
return product