Help required for writing algorithm of a program

intel1

Beta member
Messages
1
# Write algorithm to read an amount and determine the least number of currency notes to be used for the transaction of the amount.
I assumed the amount to be Rs.176 and the denominations available to be Rs.500, Rs.100, Rs.50,Rs.20,Rs.10,Rs.5 and Rs.1.
I wrote it in the following way:
step 1 start execution of program
step 2 Read A = 176
step 3 A = (500x0)+(100x1)+(50x1)+(20x1)+(10x0)+(5x1)+(1x1)
step 4 T = Least number of notes = 1+1+1+1+1 = 5
step 5 Print T
step 6 End of program

Is it right? If not, please help me to rectify my mistakes.
 
It's basically an easy idea...

you need to use a lot of loops...

I'll take your example...

curreny available = $500, $100 ,$50 ,$20 ,$10 ,$5 ,$1

and you want to make $176
as you rightly pointed out the way to make this with the least amount of notes is
$100 + $50 + $20 + $5 +$1

this is how you get to that in a program...

first you take the number that you intent to get to, and set up a cpoy of this value that you can edit
Code:
final_target = 176;
target = final_target;
and you compare that to the largest curreny avialable, if the target is larger that the currency then you take a note of how many of those bills are used until the target is smaller than that (and hence you have to move onto a small denomination of currency.
Code:
while (target >= 500)
{
target = target-500;
five_hundred_bills++;
}

then you move onto the next smallest curreny denomination

Code:
while (target >= 100)
{
target = target-100;
one_hundred_bills++;
}
then the next
Code:
while (target >= 50)
{
target = target-50;
fifty_bills++;
}

until at the end you have a final string that says.

Code:
print("in order to get %d you need to use %d Five Hundred Bills, %d Hundred Bills, %d fifty Bills, %d Twenty bills, %d Ten Bills, %d Five bills, %d One Bills" final_total, five_hundred_bills, one_hundred_bills, fifty_bills, twenty_bills, ten_bills, five_bills, one_bills);

or to get teh number of bills and not the currency you could either use this
Code:
total_bills = five_hundred_bills+one_hundred_bills+fifty_bills+twenty_bills+ten_bills+five_bills+one+bills;

print("total bills used = %d" total_bills);
or if you arn't worried about displaying what bills are used that you simply instead of using five_hundred_bills and one_hundred_bills and incrementing each one seperatly. you just have total bills. since you are only worried about the amount of bits of paper, and not their value.


here is the explenation of the algorithm with the example used.
Code:
start
|
get final amount (176)
|
save the final amount into a working target variable (176)
|
(Point A)is working target amount greater than or equal to 500? (no [176!>=500])
|
[no] -> ignore while loop and go to next part of the program
|
(Point B) Is working target greater than or equal to 100 (yes [176>=100])
|
[yes] -> add 1 to the one hundred bill counter and loop back to point b to check again, take 100 from the working target as this is now 'banked' in the hundred bill counter, (working target -> 76)
|
(point B [second itteration of while loop]) Is working target greater than or equal to 100 (no [76!>=100])
|
[no] -> go to next part of program ignoring the parts in the while loop
|
(Point C) is working total >= 50 (yes 76 > 50)
|
[yes] -> add one to the fifty bill counter, remove 50 from the working total (26) go back to point c to check again
|
(point c -second time) is working total >= 50 (no working total = 26 which is less than 50)
|
[no]-> ignore contents of while loop, move onto next part of program.
|
(point d) is working total >=20 (yes 26>20)
|
[yes] -> take twenty from the working target (working target=6) add 1 to the twenty counter, go back to point d
|
Point d (second time around), is working target >= 20 (no working target !>=20)
|
[no] -> move onto next part
|
(point e) is working target >= 10 (no)
|
[no]go to next part ignoring loop contents.
|
(point f) is working target >=5 (yes)
|
[yes]-> take five from the working target, and add five to the five_bioll counter, go back to point f
|
(point f) is working target >= 5 (no working target=1)
|
[no] ->ignore loop go on to next part
|
(point g) is working target >= 1 (yes working target =1)
|
[yes] Take away 1, (working target =0) add one to the one_bill counter, go back to point g
|
(point G, seconf time) Is working total (0) >=1 (no)
|
ignore loop contents and move on to next part of program
|
display the amount of each bill used to make the final amount. (by displaying the number in each one of the bill counters).
 
Back
Top Bottom