# Need help : fmod

So I’m making an app in my class using Codea. My teacher has explained to me that I need to use the fmod function to do something. SO, what the program does is it randomly generates a number ( 299 is the max number), and then I’m supposed to divide the number into tens hundreds and ones. Say the number is 177: I will save it as a variable, divide it by 100, and save the answer. Somehow using the fmod function, I’ll just chuck the remainder / decimal point so I know it’s a whole number: there is 1 hundred in 177. I need to somehow save that as a variable. Next, I would do the original answer (177) - (100*1) (1 comes from the number of hundreds there are which we got from the fmod function so I guess that would mean we’d need to get a variable for that too.) which is 17. Then to find the tens, I would divide 77 by 10, and go all fmod and chuck the remainder again. The answer is 7 tens in 77. I’d need to save 7, the amount of tens, as a variable somehow. Then I would do : 77 - (10 * 7) (to get the even 7 left over, because we need that to get the ones.). Finally we would do the 7/1 to find the ones, although I guess you could just use the remainder from the last thing to get the ones.

I have NO idea how to write this because I don’t understand how to use the fmod function and I’m SO confused. If I could get some help on this that’d be great :L Even an explanation would be great. Thanks Codea :)))

I’m not a mathmetician, so I can’t help, but good luck!

Thanks anyways :\ Anyone else? x.x

@JessicGriffin You spent more time writing the post saying how you would do it then it would take to actually do it. I suggest you look up math.fmod in the build in manual and understand what it actually does. I was going to post the code here, but I think the whole point is for you to figure it out on your own. That’s how you learn how individual functions work.

The first bit…

``````n=283
a=math.fmod(n,10)
n=(n-a)/10
``````

Oh Dave. Always an encouragement. Thank you, I’ll post a code when I figure it out then. Hopefully you can be of further help when I make the code @dave1707

@Ignatz Hmmm Starting to understand… Thanks!

@dave1707 how do I fmod the variable answer in the first place?

@JessicGriffin The format of the fmod command is math.fmod(x,y) which gives you the remainder of “x” divided by “y”. For example 11/4 gives 2 with a remainder of 3. So math.fmod(11,4) will give you an answer of 3, the remainder. @Ignatz gave you everything you need to know in the above post. If that is still confusing, here’s a breakdown. You need a loop to do this until you get all the digits you need.

``````
function setup()
n=283
a=math.fmod(n,10)    -- this gives you the remainder   3
n=(n-a)/10    -- this gives you the number dropping the remainder   28
print(a)    -- prints 3
print(n)    -- prints 28
end

``````

Also, the fmod function has a shortcut ‘%’ which you would use like this (taking dave’s example)

``````function setup()
n=283
a=n%10    -- notes that this is the only line that changed
n=(n-a)/10
print(a)
print(n)
end
``````

it’s not a real ‘shortcut’ but more an alternative way of writing it

I screwed up guys. I meant modf. x.x I figured out the code with a lot of help from my teacher :

``````
--# Main
-- levelApp
-- Use this function to perform your initial setup

level = 0
t=0

function setup()

supportedOrientations(LANDSCAPE_ANY)
--displayMode(FULLSCREEN)

end

-- This function gets called once every frame
function draw()
t=t+1
background(206, 33, 42, 255)
fontSize(50)
fill(11, 13, 216, 255)
font("Arial-BoldMT")
if (math.fmod (t,60)== 0) then
if (level >  15) then
level= level + 1
else if (level > 10) then
level = level + 1
else
level= level + 1
end
end

local hundredsDivision = answer/100
local hundreds = math.modf(hundredsDivision)
local hundredsRemainder = answer - (hundreds*100)

local tensDivision = hundredsRemainder/10
local tens = math.modf(tensDivision)
local ones = hundredsRemainder - (tens*10)

print(hundreds  .. " " .. tens .. " " .. ones)

end
end
``````

This code is exactly what I needed.
The next step is to print base ten blocks (hundreds, tens, one blocks) depending on the number. 283 would be 2 hundreds, 8 tens, and 3 ones.
I need to add a loop for each hundreds tens and ones, saying like for however many hundreds we need add a hundred block each time, and offset it each time… But I don’t know how to do that x.x @dave1707 , @ignatz , @stevon8er , thanks a lot. If I could get some help with my looping problem too…? :)) xD

@JessicGriffin I’m not exactly sure about everything you were doing, but at least you were trying. Compare this to yours and see what I did. I’m not sure if this is what you were trying to do, but maybe this will help you.

``````
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
font("Arial-BoldMT")
fontSize(50)
t=0
end

function draw()
background(206, 33, 42, 255)
fill(11, 216, 98, 255)
if math.fmod(t,180)==0 then    -- new number every 3 seconds
answer = math.random(100,299)  -- random 3 digit number
ones=math.fmod(answer,10) -- number of 1's
nbr=(answer-ones)/10    -- shift number 1 digit right
tens=math.fmod(nbr,10)    -- number of 10's
nbr=(nbr-tens)/10    -- shift number 1 digit right
hundreds=nbr    -- number of 100's
end
text(hundreds.." hundreds   "..tens.." tens   "..ones.." ones",WIDTH/2,HEIGHT/2-100)
t=t+1
end

``````

@dave1707 my code does the exact same thing except it sends it to the output box instead.
Now I need to make bAse ten blocks appear. This is an app designed for a grade 3. For 125, I would need one hundred block (hundred block = 10x10 block), two ten blocks (1x10 blocks) and 5 ones (5 squares). My teacher told me to do a loop to make them appear. A loop for the tens, another for hundred, another for one. I just explained the rest in my previous post… I need help if you could have a look at that @JessicGriffin I’m not sure what you’re asking. Are you saying that for 125, you need 100 blocks (squares) in a 10x10 group and then for 2 you need 2 rows of 10 blocks (squares) and for 5 you need a row of 5 blocks (squares) on the graphics screen. Then for 231 you would need 2, 10x10 groups, 3 rows of 10 blocks, then 1 block.

I think he means like this:

a block of a hundred, is 100 squares, for example each width a size of 15 width and height
they need to be placed in a 10x10 big square, so 10 rows with each 10 squares

a block of 10 = 10 square, in 1 row

and a block of 1 just equals 1 square

@JessicGriffin Here’s a start. I’ll leave the rest for you to add to your code.

``````
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
rectMode(CORNER)
end

function draw()
background(40,40,50)
text(125,300,600)
h=1
t=2
o=9
block100(h,100,100)
block10(t,400,100)
block1(o,700,100)
end

function block100(nbr,x,y)    -- number,x,y position
fill(255,0,0)
for n=1,nbr do
for a=1,10 do
for b=1,10 do
sprite("Platformer Art:Guy Standing",x+a*25,y+b*25+(n-1)*100,22,22)
end
end
end
end

function block10(nbr,x,y)    -- number,x,y position
fill(255,0,0)
for n=1,nbr do
for a=1,10 do
sprite("Platformer Art:Guy Standing",x+a*25,y+n*25,22,22)
end
end
end

function block1(nbr,x,y)    -- number blocks, x,y position
fill(255,0,0)
for a=1,nbr do
sprite("Platformer Art:Guy Standing",x+a*25,y+25,22,22)
end
end

``````

@JessicGriffin I changed the above code.

@dave1707 , I have sprites I need to insert for the blocks (from dropbox), I can’t just use the rect function. Any way to do that? :\

@JessicGriffin I modified the above code to show sprites. It was just a matter of replacing “rect” with “sprite”. I also changed the size.

@dave1707 Thanks Dave Didn’t realize it was changed, sorry 