Mod Fuction
Author |
Message |
Xypher
|
Posted: Mon Jan 28, 2008 5:23 pm Post subject: Mod Fuction |
|
|
I have an exam tomorrow and my teacher gave me a bunch of possible programs we will have to write. I've only been using Turing for a few months but I still don't know how to use the mod function.
Question @ Mon 28 Jan, 2008 5:23 pm wrote: Determine the number of toonies quarters dimes nickels and cents that must be given to a customer in change if the total amount of the change is less than five dollars. You are to give the minimum number of coins (Hint the operators div and mod may be useful)
I don't expect anybody to do this for me but I just really know how I'm suppose to use the mod function in all of this cause I'm lost.
~Thanks
Xypher
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
StealthArcher
|
Posted: Mon Jan 28, 2008 5:28 pm Post subject: RE:Mod Fuction |
|
|
The mod function is used to find the remainder in a division, so that say 41 div 4=10 yet 41 mod 4 will equal 1. Look at it for a while, the way to determine the change should come to you if not, return, and I'll help some more.
|
|
|
|
|
|
MichaelM
|
Posted: Mon Jan 28, 2008 5:35 pm Post subject: Re: Mod Fuction |
|
|
Well I havent used mod lots, but I can tell you that its purpose is to find the remainder. Example:
code: |
%To find the day of the week you're on given the day of the year
%This function will return which day # of the week you're on
var d:int
function which_day (n:int):int
result n mod 7
end which_day
loop
get d
put which_day(d)
end loop
|
So for something involving money, first youd want to find out how many whole dollars there are, say you have a "money" variable.
First of all, im sure you can find how many whole dollars you have, but to find say how many cents: cents = money mod 1 (ex 15.75 mod 1 = .75)
I hope that gives you a better understanding
|
|
|
|
|
|
MichaelM
|
Posted: Mon Jan 28, 2008 6:15 pm Post subject: Re: Mod Fuction |
|
|
Heres a better explanation. A Subraction method might make more sense to you, which is really inefficient, but gets the same result. Heres the pseudocode
code: |
loop
subtract coin amount from total
add to count of that coin
exit when total is less than coin amount
end loop
|
This is what a trace would look like:
Continue that same loop with each different coin. For example, if you had 4.97...
4.97-1 =3.97
3.97-1 =2.97
2.97-1 =1.97
1.97-1 =0.97 ->heres where it stops (4.97 mod 1 would get you here), then go subracting quarters
0.97-0.25 = 0.72
0.72-0.25 = 0.47
0.47-0.25 = 0.22 -> heres where it stops (0.97 mod .25 would get you here), then go subraction dimes
0.22-0.1 = 0.12
0.12-0.1 = 0.02 -> heres where is stops (0.22 mod 0.1 would get you here), no need to check nickels, go to pennies
0.02-0.01 = 0.01
0.01-0.01 = 0 -> The end , (.02 mod .01 brings you here).
That should make a little more sense, but I hope you see the benefit of using mod over that loop I showed you. Good Luck!
|
|
|
|
|
|
BigBear
|
Posted: Mon Jan 28, 2008 10:33 pm Post subject: Re: Mod Fuction |
|
|
This is related to mod so i am posting my question here. I wrote a cash machine program but it gets stuck and won't exit the loop. It used to work but for some reason it stopped recently. It is the loop :
loop
if change1 > 1000 then
put "\nPlease call your manager."
elsif change1 > 20 then
put "$20 bill(s): ", change1 div 20
change1 := change1 mod 20
elsif change1 = 20 then
put "$20 bill(s): ", change1 div 20
change1 := change1 mod 20
elsif change1 < 20 and change1 >= 10 then
put "$10 bill(s): ", change1 div 10
change1 := change1 mod 10
elsif change1 < 10 and change1 >= 5 then
put "$5 bill(s): ", change1 div 5
change1 := change1 mod 5
elsif change1 < 5 and change1 >= 2 then
put "$2 coins(s): ", change1 div 2
change1 := change1 mod 2
elsif change1 < 2 and change1 >= 1 then
put "$1 coins(s): ", change1 div 1
change1 := change1 mod 1
elsif change1 < 1 and change1 >= 0.25 then
put "$0.25 coin(s): ", change1 div 0.25
change1 := change1 mod 0.25
elsif change1 < .25 and change1 >= 0.10 then
put "$0.10 coin(s): ", change1 div 0.10
change1 := change1 mod 0.10
elsif change1 < .10 and change1 >= 0.05 then
put "$0.05 coin(s): ", change1 div 0.05
change1 := change1 mod 0.05
elsif change1 < .05 and change1 >= 0.01 then
put "$0.01 coin(s): ", change1 div 0.01
change1 := change1 mod 0.01
elsif change1 = 0.01 then
put "$0.01 coin(s): ", change1 div 0.01
change1 := change1 mod 0.01
elsif change < 0.01 then
change := 0
end if
exit when change1 = 0 or change1 > 1000
end loop
Description: |
|
Download |
Filename: |
Manuel.txt |
Filesize: |
1.45 KB |
Downloaded: |
285 Time(s) |
Description: |
|
Download |
Filename: |
CashMachineAccessCode.txt |
Filesize: |
3 Bytes |
Downloaded: |
225 Time(s) |
Description: |
|
Download |
Filename: |
CashMachineRecord.txt |
Filesize: |
13 Bytes |
Downloaded: |
201 Time(s) |
Description: |
|
Download |
Filename: |
Cashiers.txt |
Filesize: |
27 Bytes |
Downloaded: |
261 Time(s) |
Description: |
|
Download |
Filename: |
Cash Machine.t |
Filesize: |
13.04 KB |
Downloaded: |
293 Time(s) |
|
|
|
|
|
|
MichaelM
|
Posted: Tue Jan 29, 2008 9:31 am Post subject: Re: Mod Fuction |
|
|
Hmm, I'll take a closer look in a little while, try and check back later. (I have three exams in the next three days )
PS: When posting code it helps to use the code tags:
|
|
|
|
|
|
BigBear
|
Posted: Wed Jan 30, 2008 8:44 pm Post subject: Re: Mod Fuction |
|
|
Well if no one else see this dire request for help I will post it in a new topic.
|
|
|
|
|
|
|
|