Computer Science Canada Lowest amount of bills/coins |
Author: | Slnj [ Sat Mar 27, 2010 9:31 pm ] | ||
Post subject: | Lowest amount of bills/coins | ||
What is the problem you are having? Im stuck Questions : Write a program that inputs a dollar amount and outputs how the amount can be represented using the fewest number of bills and coins. Hint: Use the mod and div commands. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) I have tried doing it on my own but got stuck beacuse say if i put $150 as the amount of money it will only says 1 hundred dollar bill and it won't say 1 fifty dollar bill.
Please specify what version of Turing you are using latest version 4.11 i think |
Author: | Tony [ Sat Mar 27, 2010 9:38 pm ] |
Post subject: | RE:Lowest amount of bills/coins |
One you take out that one $100 bill, how do you know that you have $50 more left? (That answer is not described in your code) |
Author: | Shah-Cuber [ Sat Mar 27, 2010 9:48 pm ] |
Post subject: | Re: Lowest amount of bills/coins |
A good way to learn Dynamic Programming (well-known problem). I'll let you figure it out ... |
Author: | Slnj [ Sun Mar 28, 2010 7:28 am ] | ||
Post subject: | RE:Lowest amount of bills/coins | ||
ok so tried again i thought i got it but then i almost got it =/ here's what my code looks like can guy's see what i did wrong or what should add in there?
look at the image i put 0.15 but it give me 4 pennies and 1 dime? what did i do wrong ![]() ![]() |
Author: | chrisbrown [ Sun Mar 28, 2010 11:02 am ] |
Post subject: | RE:Lowest amount of bills/coins |
Interesting, its caused by floating-point error. I haven't seen such a simple example of it. Anyway, you didn't do anything wrong. Since decimals aren't exact, when you expect money to have the value 0.05, it might actually be more like 0.0499999, so the if condition is false. After you get money, multiply it by 100, and change your values to compare based on cents, not dollars. |
Author: | Slnj [ Sun Mar 28, 2010 11:20 am ] |
Post subject: | Re: RE:Lowest amount of bills/coins |
methodoxx @ Sun Mar 28, 2010 11:02 am wrote: Interesting, its caused by floating-point error. I haven't seen such a simple example of it.
Anyway, you didn't do anything wrong. Since decimals aren't exact, when you expect money to have the value 0.05, it might actually be more like 0.0499999, so the if condition is false. After you get money, multiply it by 100, and change your values to compare based on cents, not dollars. can't do that because i need to use dollars. so i guess i did it everything right? |
Author: | chrisbrown [ Sun Mar 28, 2010 11:34 am ] |
Post subject: | Re: RE:Lowest amount of bills/coins |
Slnj @ Sun Mar 28, 2010 11:20 am wrote: can't do that because i need to use dollars. so i guess i did it everything right?
Well, it's right in the sense that it would work in an ideal world, but since it doesn't produce correct answers, it's not really right. There's nothing stopping you from dividing by 100 afterwards to convert back into dollars. |
Author: | Euphoracle [ Sun Mar 28, 2010 1:57 pm ] |
Post subject: | RE:Lowest amount of bills/coins |
Multiply all your values by 100, including your input value. This should eliminate the floating point errors. eg. Checking for $100 is now checking for '10000'; checking for pennies, or $0.01, is now checking for '1'. Your input, if it was $15.99, would now be '1599' |
Author: | Slnj [ Mon Mar 29, 2010 2:23 pm ] |
Post subject: | RE:Lowest amount of bills/coins |
Thanks guy's i managed to do what you guy's said with some help from some people in the chat room. ![]() |