Posted: 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 exitwhen n = nm
endloop
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
andrew.
Posted: 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 exitwhen n = nm
sum := sum + x**n
n := n + 1 endloop
comp_help
Posted: Mon Apr 13, 2009 5:08 pm Post subject: RE:turing - help w terms
andrew thanks for that, now it does work for 2
But it doesn't seem to work for 1. Should/Can I put an exit statement outside(before) the loop.
comp_help
Posted: Mon Apr 13, 2009 5:12 pm Post subject: Re: turing - help w terms
Nvm, I got it working
I removed the code outside the loop and it seems to be working.
Thanks anyway andrew.
andrew.
Posted: Mon Apr 13, 2009 7:17 pm Post subject: RE:turing - help w terms