Jump to content




even and odd numbers?


12 replies to this topic

#1 ChiknNuggets

  • Members
  • 130 posts

Posted 30 December 2012 - 06:14 AM

so i got a table and it goes something like this table = {time,value,time,value,time,value,...}
and what i need is a way so that it goes something like this

if odd number sleep(time)
if even number note(value)

but i cant seem to figure out a way to check if the current number is a odd or even number, in something like this

for i=1, #table do


end

Any help is much appreciated

#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 30 December 2012 - 06:23 AM

You can use math.fmod()

t_table = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

for i = 1, #t_table do
    if t_table[i] % 2 == 0 then -- This is another way of using math.fmod, it's the same as 'if math.fmod(t_table[i], 2) == 0 then'
        print(t_table[i] .. " is even!")
    else
        print(t_table[i] .. " is odd!")
    end
end


#3 ChiknNuggets

  • Members
  • 130 posts

Posted 30 December 2012 - 06:23 AM

dw found it



if math.mod(num, 2) == 0 then
print("even")
else
print("odd")
end

didnt realise there was a math funtion to check for remainders

#4 Cozzimoto

  • Members
  • 221 posts
  • LocationDallas, Tx

Posted 30 December 2012 - 06:23 AM

use Modulus [%]

its basically finds the remainder of a dividend and returns that remainder.

IE:
1%2 = 1
2%2 = 0
3%2 = 1
4%2 = 0
etc...

so you can use the 0s and 1s to alternate in your table

Modulus is so much fun and makes programming math in technically any programming language easier

#5 ChiknNuggets

  • Members
  • 130 posts

Posted 30 December 2012 - 06:24 AM

yours does save me the time of changeing this to fit my method, thank you very much

#6 Cozzimoto

  • Members
  • 221 posts
  • LocationDallas, Tx

Posted 30 December 2012 - 06:24 AM

wow we all replied at the same time! =P

#7 GopherAtl

  • Members
  • 888 posts

Posted 30 December 2012 - 06:24 AM

i%2 will return 0 if even, 1 if odd.

:Edit: wow, 10 minutes of nothing then ninjaspamfest, lol.

#8 Cozzimoto

  • Members
  • 221 posts
  • LocationDallas, Tx

Posted 30 December 2012 - 06:27 AM

thats what i love about this community, help at the ready INSTANTLY

sorry off topic! lol =P

#9 ChiknNuggets

  • Members
  • 130 posts

Posted 30 December 2012 - 06:40 AM

actually i ran into a problem so while i got ur attention
	   for i = 1, #song do
  if song[i] % 2 == 0 then
   play(song[i])
  else
   sleep(song[i])
	 end
end


it doesnt seem to want to work, as in it skips the parts that would go if it werent in a sleep its really wierd

#10 ChiknNuggets

  • Members
  • 130 posts

Posted 30 December 2012 - 06:42 AM

dw fixed that too for some reason this works but that doesnt
for i,v in ipairs(song) do
if i % 2 == 0 then
play(v)
else
sleep(v)
end
end

#11 Cozzimoto

  • Members
  • 221 posts
  • LocationDallas, Tx

Posted 30 December 2012 - 06:42 AM

did you try reversing what you got, are you sure you got the right argument types in the functions you are passing through

a modulus returning 0 should be an even number. make sure its a number type though

#12 Cozzimoto

  • Members
  • 221 posts
  • LocationDallas, Tx

Posted 30 December 2012 - 06:43 AM

yea tables are a pain at times if you didnt realize you were trying to compare multiple args to one

#13 ChiknNuggets

  • Members
  • 130 posts

Posted 30 December 2012 - 06:45 AM

yea idk what it was but now ive finished a touch screen piano that can record songs and play them back at the exact same speed at which you recorded it at





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users