Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Even/Odd If Statements
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zampano




PostPosted: Wed Sep 19, 2007 7:46 am   Post subject: Even/Odd If Statements

Hey guys, I just started in Turing, and because I finished the class's assigned programming my teacher gave me a bonus question to solve which involves code I don't know about.
The assignment is to create a two-player game with alternating turns (the rest of the programming assignment is irrelevant) and I am not sure how I could reuse the game's code and still have alternating turns on that game.
My question eventually boils down to this:
How can I ask if a number is even in an if statement?
What I tried was:

tries:=tries+1

and later:

if tries=even then
put "Player two, go."

However, nothing I tried worked. Is there a simpler way? Thanks guys.
Sponsor
Sponsor
Sponsor
sponsor
neufelni




PostPosted: Wed Sep 19, 2007 7:59 am   Post subject: RE:Even/Odd If Statements

You will need to use the mod command. It determines the remainder of a division. And since all even numbers have a remainder of 0 when divided by 2, it's as simple as this:
Turing:
if num mod 2 = 0 then
    put "Even"
else
    put "Odd"
end if
Zampano




PostPosted: Wed Sep 19, 2007 8:31 am   Post subject: Re: Even/Odd If Statements

I cannot thank you enough.
Lol, the class I had to do it in hadn't even finished and you responded so quickly.
Thanks.
Tony




PostPosted: Wed Sep 19, 2007 12:23 pm   Post subject: RE:Even/Odd If Statements

Just no cheating, ok? Wink

Another really cool way of alternating turns has to do with the property that -1 * -1 = 1. So instead of incrementing the step and figuring out the remainder each time, you just flip the sign.
Turing:

var turn : int := 1
loop
   if turn > 0 then
      put "Player 1"
   else
      put "Player 2"
   end if

   turn *= -1
end loop
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Bored




PostPosted: Thu Sep 27, 2007 9:59 am   Post subject: Re: Even/Odd If Statements

Or along the same idea, you could use a boolean value and change it each time.
Turing:
var playerOne : boolean := true
loop
    if playerOne  then
        put "Player 1"
    else
        put "Player 2"
    end if

    playerOne  := not playerOne %True becomes false or false becomes true
end loop
Zampano




PostPosted: Thu Sep 27, 2007 10:09 am   Post subject: Re: Even/Odd If Statements

No cheating: goes without saying.
The assignment is not an inclass test, but rather the question was assigned in class, and I got a response before that same class ended, which meant I was able to make the whole program in one class.
Thanks again guys!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: