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

Username:   Password: 
 RegisterRegister   
 Help with a certain code
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
karseet




PostPosted: Wed Dec 28, 2005 1:17 pm   Post subject: Help with a certain code

Hi,
I am currently working on a program which simulates something like a cafeteria experience, where a user selects their meal, pays for it obtains receipt, etc..
Part of the program is the amount rendered (amount of cash you pay to the clerk at the cashier). For example, the total price is 11.60 and I render 20 dollars to the program, I will receive $9.40 change in return.
My question is if I get $4.40 in change, for example, how would I display how much of specific coins/bills the person would get back. In this case I would get back something like one 4 dollar bill, two toonies, a quarter, a nickel and a dime. How could I program it so the program would display the change (which I can do) and THEN display which coins and bills the person received.
Thanks in advance, Very Happy
Joe
Sponsor
Sponsor
Sponsor
sponsor
karseet




PostPosted: Wed Dec 28, 2005 1:20 pm   Post subject: my bad

Two minor mistakes in my post:
Hi,
I am currently working on a program which simulates something like a cafeteria experience, where a user selects their meal, pays for it obtains receipt, etc..
Part of the program is the amount rendered (amount of cash you pay to the clerk at the cashier). For example, the total price is 11.60 and I render 20 dollars to the program, I will receive $9.40 change in return.
My question is if I get $9.40 in change, for example, how would I display how much of specific coins/bills the person would get back. In this case I would get back something like one 5 dollar bill, two toonies, a quarter, a nickel and a dime. How could I program it so the program would display the change (which I can do) and THEN display which coins and bills the person received.
Thanks in advance, Very Happy
Joe
Tony




PostPosted: Wed Dec 28, 2005 1:49 pm   Post subject: (No subject)

you start with highest bill available, and see how many times you can pay with that, without overpaying.

for $9.40
could you pay in $20s? no
$10s? no
$5? yes. Remainder is $4.40
$2? yes. Remainder is $2.40
can you pay in $2 again? yes. Remainder is $0.40
etc.

The commands you're looking for are mod for remainder and div for integer division.

It might be best to represent prices in cents, to avoid decimal places.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Albrecd




PostPosted: Wed Dec 28, 2005 3:58 pm   Post subject: (No subject)

You could devide the total by the highest value that can be held by a bill (round down), then multiply the quocient by the value of that bill, then subtract the product from the inputted value, then do the same with the other bill and coin values in decending order.

Ex:
code:


var input, NumBill50, NumBill50Round : real
var NumBills : int
Const DOLLARS50 := 50

NumBill50 := input / DOLLARS50     %Devide the input by 50 for $50 bill
NumBill50Round := input div DOLLARS50 %Same as ^ with rounding
if NumBill50 < NumBill50Round then           %If the unrounded quocient is
NumBill50Round -= 1                                %Lower than the rounded
end if                                                       %quocient, It has rounded up,
%Keep Track of the number of $50 bills...   %but it must round down so
                                                               %we subtract one.

input -= NumBill50Round

%Then do the same thing with the next bill or coin value and so on
karseet




PostPosted: Fri Dec 30, 2005 10:47 pm   Post subject: Thanks

All right, thanks Tony and Albrecd (and anyone other member willing to help)!
The mod function really confuses me, so I will stay away from it (for now), as I am only a beginner programmer.
As for the code which Albrecd has posted, will copying the code, declaring new variables, work for every type of coin/bill? If so, how would I collaborate all the codes together, after the logic has been done, and make the program output what it has just calculated?
Thanks guys,
Joe
do_pete




PostPosted: Sat Dec 31, 2005 2:42 am   Post subject: (No subject)

Albrecd, those if statemdnts can be replaced with:
code:
floor (NumBill50)

floor rounds down and ceil rounds up
Albrecd




PostPosted: Sun Jan 01, 2006 12:52 am   Post subject: (No subject)

Quote:
will copying the code, declaring new variables, work for every type of coin/bill? If so, how would I collaborate all the codes together, after the logic has been done, and make the program output what it has just calculated?


yes, to display the bills and coins at the end, you just put NumBill50, NumBill20, NumBill10, NumBill5, etc... down to the coins.

Note that the code will have to be altered slightly for the coins (and do_pete is right, you can use the floor function instead of the if statements)
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  [ 7 Posts ]
Jump to:   


Style:  
Search: