N M
asked on
Algorithm needed to calculate cashier changes..
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
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
ASKER
Hello,
THANK you for your comment, this is true, the algorithm has been invented for this particular coin series. Possible other variations will null it, like if you have just one coin of 7 cents, or if you have only 2 coins of 7 and 11 cents (both primes) etc. So, for the time being, please accept the specific batch of coins as standard.
The calculation algorithm should be 6 steps, meaning that when you start to calculate the coins needed for changes up to the moment that you will have the answer in some register, 6 commands will be used. Actually, this is the heart of the problem, since I've found the algorithm.
Nick
THANK you for your comment, this is true, the algorithm has been invented for this particular coin series. Possible other variations will null it, like if you have just one coin of 7 cents, or if you have only 2 coins of 7 and 11 cents (both primes) etc. So, for the time being, please accept the specific batch of coins as standard.
The calculation algorithm should be 6 steps, meaning that when you start to calculate the coins needed for changes up to the moment that you will have the answer in some register, 6 commands will be used. Actually, this is the heart of the problem, since I've found the algorithm.
Nick
I don't know much about x86 assembly but I can give some ideas about the algorithm.
1. Divide the change number by 25. The least significant bit will be the number of quarters (0 or 1). Shifted right one bit will be the number of fifty-cent pieces.
2. Multiply the result from step 1 by 25.
3. Subtract the result from 2 from the original value.
4. Divide that by 5. The least significant bit will be the number of nickles (0 or 1). Shifted right one bit will be the number of dimes (0, 1, or 2).
5. Multiply the result from step 4 by 5.
6. Subtract the result from step 5 from the value derived in step 3. That is the number of pennies.
Given 67 cents:
Step 1 gives 2 (10 binary) which is one fifty-cent piece (1x) and zero quarters (0x).
Step 2 gives 50 (2 * 25)
Step 3 gives 17 (67 - 50)
Step 4 gives 3 (011 binary) which is one dime (01x) and one nickle (xx1).
Step 5 gives 15 (3 * 5)
Step 6 gives 2 (17 - 15)
So you need one fifty-cent piece, zero quarters, one dime, one nickle, and two pennies to equal 67 cents.
I am assuming that bit shifting and masking do not count in the limit of 6 math steps.
1. Divide the change number by 25. The least significant bit will be the number of quarters (0 or 1). Shifted right one bit will be the number of fifty-cent pieces.
2. Multiply the result from step 1 by 25.
3. Subtract the result from 2 from the original value.
4. Divide that by 5. The least significant bit will be the number of nickles (0 or 1). Shifted right one bit will be the number of dimes (0, 1, or 2).
5. Multiply the result from step 4 by 5.
6. Subtract the result from step 5 from the value derived in step 3. That is the number of pennies.
Given 67 cents:
Step 1 gives 2 (10 binary) which is one fifty-cent piece (1x) and zero quarters (0x).
Step 2 gives 50 (2 * 25)
Step 3 gives 17 (67 - 50)
Step 4 gives 3 (011 binary) which is one dime (01x) and one nickle (xx1).
Step 5 gives 15 (3 * 5)
Step 6 gives 2 (17 - 15)
So you need one fifty-cent piece, zero quarters, one dime, one nickle, and two pennies to equal 67 cents.
I am assuming that bit shifting and masking do not count in the limit of 6 math steps.
ASKER
Your answer is correct. Yes, shifting and masking do not count in the limit of 6 math steps.
May I have the source code in assembly please? (I think the points are yours already).
THANK you
NICK
May I have the source code in assembly please? (I think the points are yours already).
THANK you
NICK
I can try but it's been years since I've done anything in assembly. Wish me luck :)
ASKER
Wishing you luck from bottom of my heart (plus crossing fingers..)
:-)
Nick
:-)
Nick
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Couldn't even wish for something better.
Your help is INVALUABLE.
THANK YOU SO MUCH for your effort.
Going for test immediately (grin)
NICK
Your help is INVALUABLE.
THANK YOU SO MUCH for your effort.
Going for test immediately (grin)
NICK
Also, I don't understand what you mean with "the calculation algorithm should be no more than 6 steps"