Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 08-13-2006, 03:34 AM   #1
Beta Member
Join Date: Aug 2006
Posts: 1
Question Help required for writing algorithm of a program

# 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.

intel is offline   Reply With Quote
Old 08-13-2006, 06:28 PM   #2
Golden Master
borat_sagdiyev's Avatar
Join Date: Feb 2006
Posts: 8,986
Send a message via AIM to borat_sagdiyev Send a message via MSN to borat_sagdiyev
Default Re: Help required for writing algorithm of a program

uhhh, what?

Core 2 Duo e4500 2.2ghz @ 2.8ghz
evga 650i ultra
2gb 400mhz ram OC'ed to 450
evga geforce 7600GT overclocked
borat_sagdiyev is offline   Reply With Quote
Old 08-14-2006, 06:46 AM   #3
Site Team
root's Avatar
Join Date: Mar 2004
Posts: 8,098
Default Re: Help required for writing algorithm of a program

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
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.
while (target >= 500)
target = target-500;
then you move onto the next smallest curreny denomination

while (target >= 100)
target = target-100;
then the next
while (target >= 50)
target = target-50;
until at the end you have a final string that says.

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
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.
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).
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 01:07 AM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0