Computer Science Canada change sorter always prints at least 1 |
Author: | Insectoid [ Tue Apr 08, 2008 9:59 am ] | ||
Post subject: | change sorter always prints at least 1 | ||
I am writing a project that will take a cash value and print out how many of each coin/bill you need. the problem is, it always shows at least 1 of each bill. So, here's my code!
my guess is that I have to make the 'count:=count+1' skip the first time it loops, but I don't know how. |
Author: | Zampano [ Tue Apr 08, 2008 10:20 am ] |
Post subject: | Re: Difficult to describe... |
Right, as the failure of the exit statement itself affirms that there indeed was more than enough to pay a bill, your ifs don't need to be there. The reason that you get at least one is the exit is after the increment of "count%%%%%". What you want to do is make the exit the first thing, so that counttwenties or such will not increment if there isn't enough for a bill in the first place. How do you think you could make it so that the exit will be the first thing to execute? If the exit returns false, then we will know for sure that there is enough for the amount of the monetary unit to go up one. So you can use the exit statement like an inclusion guard. If the program passes the exit, it knows for sure that it should add to count%%%%%, and thus it needs no ifs. Also, you should think of using an array in a way that you only need to use one loop. |
Author: | Insectoid [ Tue Apr 08, 2008 10:33 am ] |
Post subject: | RE:change sorter always prints at least 1 |
Um..We haven't done arrays yet, and I just read part of the chapter on array a few minutes ago and have yet to try it out. Thanks. |
Author: | DaveAngus [ Tue Apr 08, 2008 12:36 pm ] |
Post subject: | RE:change sorter always prints at least 1 |
when I type in $100 (total amount of money) I get 5 twenties and 1 of everything else. Look over you code so that you can make it so it will spit out the proper amount of each coin. And arrays are nice to learn, so try to learn them as fast as possible because it will clean up your code. If you need any help just message me alright? Take care |
Author: | DaveAngus [ Tue Apr 08, 2008 12:37 pm ] |
Post subject: | RE:change sorter always prints at least 1 |
Oh sorry bud I didnt read the question. hahahaha. So sorry. Umm the reason why your sorter always prints a 1 because you have +1 after your counters. you should remove those if you dont want a 1 for every total! Once again sorry about that. hahaha |
Author: | Insectoid [ Wed Apr 09, 2008 8:07 am ] | ||
Post subject: | Re: change sorter always prints at least 1 | ||
Okay, I fixed that problem, but now I have another problem, where the answer is SOMETIMES a penny short, and sometimes dead-on. (19.99 show 3 pennies, 49.99 is correct)
|
Author: | DaveAngus [ Wed Apr 09, 2008 8:16 am ] |
Post subject: | RE:change sorter always prints at least 1 |
I didnt have that pissue. Ill keep trying and Ill try to de bug it! Congrats! |
Author: | Insectoid [ Wed Apr 09, 2008 8:23 am ] |
Post subject: | Re: change sorter always prints at least 1 |
Go ahead and debug it, but don't post your revised code (lots of complaints heard about you...). Just tell me what's wrong, and how to fix it, and why your way works.. |
Author: | OneOffDriveByPoster [ Wed Apr 09, 2008 9:22 am ] |
Post subject: | Re: change sorter always prints at least 1 |
Okay; you are being hit by inaccuracy caused by floating point arithmetic. Express your values in cents and switch to using ints. |
Author: | Insectoid [ Wed Apr 09, 2008 10:26 am ] |
Post subject: | RE:change sorter always prints at least 1 |
Thanks! Completely perfect now! |