change sorter always prints at least 1
Author |
Message |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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!
code: |
% Variable & constant declarations
var total : real
const twenties : real := 20
const tens : real := 10
const fives : real := 5
const toonies : real := 2
const loonies : real := 1
const quarters : real := 0.25
const dimes : real := 0.1
const nickels : real := 0.05
const pennies : real := 0.01
var counttwenties : int := 0
var counttens : int := 0
var countfives : int := 0
var counttoonies : int := 0
var countloonies : int := 0
var countquarters : int := 0
var countdimes : int := 0
var countnickels : int := 0
var countpennies : int := 0
%Prompt user for cash value
put "Enter the total coin value"
get total
%loop to find # of twenties
loop
if total >= twenties then
total := total - twenties
end if
counttwenties := counttwenties + 1
exit when total < twenties
end loop
%print out # of twenties
put "There are ", counttwenties, " twenty dollar bills"
%loop to find number of tens
loop
if total >= tens then
total := total - tens
end if
counttens := counttens + 1
exit when total < tens
end loop
%print out number of tens
put "there are ", counttens, " ten dollar bills"
loop
if total >= fives then
total := total - fives
end if
countfives := countfives + 1
exit when total < fives
end loop
%print fives
put "there are ", countfives, " five dollar bills"
%toonies loop
loop
if total >= toonies then
total := total - toonies
end if
counttoonies := counttoonies + 1
exit when total < toonies
end loop
%print toonies
put "There are ", counttoonies, " toonies."
% loop loonies
loop
if total >= loonies then
total := total - loonies
end if
countloonies := countloonies + 1
exit when total < loonies
end loop
% print loonies
put "there are ", countloonies, " loonies"
%quarters loop
loop
if total >= quarters then
total := total - quarters
end if
countquarters := countquarters + 1
exit when total < quarters
end loop
% print quarters
put "there are ", countquarters, " quarters."
%dimes loop
loop
if total >= dimes then
total := total - dimes
end if
countdimes := countdimes + 1
exit when total < dimes
end loop
%print dimes
put "there are ", countdimes, " dimes."
%nickels loop
loop
if total >= nickels then
total := total - nickels
end if
countnickels := countnickels + 1
exit when total < nickels
end loop
%print nickels
put "there are ", countnickels, " nickels."
% pennies loop
loop
if total >= pennies then
total := total - pennies
end if
countpennies := countpennies + 1
exit when total < pennies
end loop
% print pennies
put "there are ", countpennies, " pennies."
|
my guess is that I have to make the 'count:=count+1' skip the first time it loops, but I don't know how. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Zampano
![](http://compsci.ca/v3/uploads/user_avatars/123384421747e86c6cddac1.gif)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
DaveAngus
![](http://compsci.ca/v3/uploads/user_avatars/121496826447fbae72bc179.gif)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
DaveAngus
![](http://compsci.ca/v3/uploads/user_avatars/121496826447fbae72bc179.gif)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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)
code: |
% Variable & constant declarations
var total : real
const twenties : real := 20
const tens : real := 10
const fives : real := 5
const toonies : real := 2
const loonies : real := 1
const quarters : real := 0.25
const dimes : real := 0.1
const nickels : real := 0.05
const pennies : real := 0.01
var counttwenties : int := 0
var counttens : int := 0
var countfives : int := 0
var counttoonies : int := 0
var countloonies : int := 0
var countquarters : int := 0
var countdimes : int := 0
var countnickels : int := 0
var countpennies : int := 0
%Prompt user for cash value
put "Enter the total coin value"
get total
%loop to find # of twenties
loop
exit when total < twenties
total := total - twenties
counttwenties := counttwenties + 1
end loop
%print out # of twenties
put "There are ", counttwenties, " twenty dollar bills"
%loop to find number of tens
loop
exit when total < tens
total := total - tens
counttens := counttens + 1
end loop
%print out number of tens
put "there are ", counttens, " ten dollar bills"
loop
exit when total < fives
total := total - fives
countfives := countfives + 1
end loop
%print fives
put "there are ", countfives, " five dollar bills"
%toonies loop
loop
exit when total < toonies
total := total - toonies
counttoonies := counttoonies + 1
end loop
%print toonies
put "There are ", counttoonies, " toonies."
% loop loonies
loop
exit when total < loonies
if total >= loonies then
total := total - loonies
end if
countloonies := countloonies + 1
end loop
% print loonies
put "there are ", countloonies, " loonies"
%quarters loop
loop
exit when total < quarters
total := total - quarters
countquarters := countquarters + 1
end loop
% print quarters
put "there are ", countquarters, " quarters."
%dimes loop
loop
exit when total < dimes
total := total - dimes
countdimes := countdimes + 1
end loop
%print dimes
put "there are ", countdimes, " dimes."
%nickels loop
loop
exit when total < nickels
total := total - nickels
countnickels := countnickels + 1
end loop
%print nickels
put "there are ", countnickels, " nickels."
% pennies loop
% Bug- sometimes a penny short
loop
exit when total < pennies
total := total - pennies
countpennies := countpennies + 1
end loop
% print pennies
put "there are ", countpennies, " pennies."
|
|
|
|
|
|
![](images/spacer.gif) |
DaveAngus
![](http://compsci.ca/v3/uploads/user_avatars/121496826447fbae72bc179.gif)
|
Posted: 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! |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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.. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Wed Apr 09, 2008 10:26 am Post subject: RE:change sorter always prints at least 1 |
|
|
Thanks! Completely perfect now! |
|
|
|
|
![](images/spacer.gif) |
|
|