Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 help on turing code (how to figure out exact change)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sakurasan1122




PostPosted: Mon Jan 07, 2008 4:26 pm   Post subject: help on turing code (how to figure out exact change)

so im making a program that allows you to choose items from a menu and it calculates the total. the user enters the tendered amount and it shows the change. what i need to know is how i can make the program figure out the actual coins and bills involved with the change?
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Mon Jan 07, 2008 5:01 pm   Post subject: Re: help on turing code

Try to implement this:
code:
Money: 20, 10, 5, 2, 1, 0.25, 0.1, 0.05, 0.01
24.45 - 20    =   4.45 above zero, uses one 20
 4.45 - 20    = -15.55 below zero, try next
 4.45 - 10    =  -5.55 below zero, try next
 4.45 -  5    =  -0.55 below zero, try next
 4.45 -  2    =   2.45 above zero, uses one 2
 2.45 -  2    =   0.45 above zero, uses another 2
 0.45 -  2    =  -1.55 below zero, try next
 0.45 -  1    =  -0.55 below zero, try next
 0.45 -  0.25 =   0.20 above zero, uses one 0.25
 0.20 -  0.25 =  -0.05 below zero, try next
 0.20 -  0.1  =   0.10 above zero, uses one 0.1
 0.10 -  0.1  =   0.00 equals zero, uses another 0.1, done
CodeMonkey2000




PostPosted: Mon Jan 07, 2008 5:07 pm   Post subject: RE:help on turing code (how to figure out exact change)

Don't subtract, but rather take the integer part of cash divided by bill.
ericfourfour




PostPosted: Mon Jan 07, 2008 5:28 pm   Post subject: Re: help on turing code (how to figure out exact change)

Good idea CodeMonkey2000. Truncation (div) will help out with that implementation as well:
code:
24.45 div 20.00 = 1; 24.45 - (20.00 * 1) = 4.45; uses one 20
 4.45 div 10.00 = 0
 4.45 div  5.00 = 0
 4.45 div  2.00 = 2;  4.45 - ( 2.00 * 2) = 0.45; uses two 2
 0.45 div  1.00 = 0
 0.45 div  0.25 = 1;  0.45 - ( 0.25 * 1) = 0.20; uses one 0.25
 0.20 div  0.10 = 2;  0.20 - ( 0.10 * 2) = 0.00; uses two 0.10
 done!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: