Below is the pseudocode for a greedy algorithm for making change given total amount n and r coins of denomination C1, C2, Cr:
Change making algorithm in pseudocode procedure change (C1, C2, ..., Cr, n: a positive integer)
for i:= 1 to r do
di:= 0 {di counts the number of coins of denomination Ci used}
while n >= Ci do
n := n - Ci
di := di + 1 {Add one coin of denomination Ci}
end while
return d1, d2, ..., dr {di is the number of coins of denomination Ci used}
In the particular case where the four denominations are quarters, dimes, nickels, and pennies, we have C1 = 25, C2 = 10, C3 = 5, and C4 = 1. Trace the algorithm by filling the table below for change(25, 10, 5, 1, 67):
i | d1 | d2 | d3 | d4 | n > Ci?
-----------------------------
1 | 2 | 1 | 1 | 2 | True
Write the pseudocode of an algorithm covered in lecture that uses the same approach as the one presented above but solves a different problem. Describe the main differences between the two algorithms.