Solved

# Algorithm needed to calculate cashier changes..

Posted on 2006-10-31

Hello to you all,

The problem is this: I need a small assembly program to calculate cashier's changes R using coins of 50, 20, 10, 5, 2 and 1 cent and only those ones (on any quantity of course). I know from the beginning that 0 < R < 100 and R is integer. Two constraints apply: (a) I shall use the minimum set of coins (i.e. the 50 cent coin is prefered over 2 twenties and 1 ten etc.) and (b) the calculation algorithm should be no more than 6 steps. I have found that the algorithm is

X = R - [(MOD R,Cn)] / Cn

in a loop, where X is each time the amount of coins (in 1st loop are 50s, 2nd loop are 20s etc), R are the changes (i.e. 94 cents etc) and C is the coin used every time (C1 = 50, C2 = 20, C3 = 10 etc) while n is the repetition (1 to 6).

Example: Suppose that changes that should be given are 77 cents and coins are stored in C1, C2 etc.. (C1=50, C2=20, C3=10.. etc).

See the loop:

X1 = [77 - (MOD 77, 50)] / 50 = [77 - 27] / 50 = 50 / 50 = 1 (one coin of 50 cents should be given).

R = R - (X1 * C1) = 77 - (1 * 50) = 27

X2 = [27 - (MOD 27,20)] / 20 = [27 - 7] / 20 = 20 / 20 = 1 (one coin of 20 cents should be given).

R = R - (X2 * C2) = 27 - (1 * 20) = 7

X3 = [7 - (MOD 10, 7)] / 10 = [7 - 7] / 10 = 0 / 10 = 0 (no coins of 10 cents should be given).

R = R - (X3 * C3) = 7 - (0 * 10) = 7

X4 = [7 - (MOD 7, 5)] / 5 = [7 - 2] / 5 = 5 / 5 = 1 (one coin of 5 cents should be given).

R = R - (X4 * C4) = 7 - (1 * 5) = 7 - 5 = 2

etc.

The algorithm is working, but, how do I do this in assembly? And calculation steps to be no more than 6 (the algorithm).

The little program should run on Intel machine. Any help would be appreciated (I use masm but I can use any tool you dictate - THANK you in advance).

Nick