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

Username:   Password: 
 RegisterRegister   
 turing - help w terms
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
comp_help




PostPosted: Mon Apr 13, 2009 3:59 pm   Post subject: turing - help w terms

What is it you are trying to achieve?
I am trying to make this program work with user input for the number of terms which is less than 3.

What is the problem you are having?
This code doesn't work if the user input is less than 3 for the number of terms.


Describe what you have tried to solve this problem
I have made a code.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Code is below:

Turing:


var x: int
var nm: int
var n: int:= 0
var sum: int:= 0

put "1 + x + x**2 + x**3 + x**4 + ..."
put "\nEnter a value for x:"
get x
put "Enter the number of terms (n):"
get nm

sum:= sum + 1
n:= n + 1
sum:= sum + x
n:= n + 1


loop
sum:= sum + x**n
n:= n + 1
exit when n = nm
end loop

put "\nThe sum of the nth term is:"
put sum




Please specify what version of Turing you are using
Turing 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
andrew.




PostPosted: Mon Apr 13, 2009 4:12 pm   Post subject: RE:turing - help w terms

It doesn't work because you start n off with 2 before you go into the loop. If you put 2 or less, then the loop will never exit and you will have an overflow. You have to put the exit statement first in the loop.

Turing:
loop
exit when n = nm
sum := sum + x**n
n := n + 1
end loop
comp_help




PostPosted: Mon Apr 13, 2009 5:08 pm   Post subject: RE:turing - help w terms

andrew thanks for that, now it does work for 2 Smile
But it doesn't seem to work for 1. Should/Can I put an exit statement outside(before) the loop.
comp_help




PostPosted: Mon Apr 13, 2009 5:12 pm   Post subject: Re: turing - help w terms

Nvm, I got it working Very Happy

I removed the code outside the loop and it seems to be working.

Thanks anyway andrew.
andrew.




PostPosted: Mon Apr 13, 2009 7:17 pm   Post subject: RE:turing - help w terms

No problem.
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  [ 5 Posts ]
Jump to:   


Style:  
Search: