Computer Science Canada Any Help Appreciated |
Author: | DstSoldier [ Wed Mar 19, 2008 6:32 pm ] |
Post subject: | Any Help Appreciated |
I'm somewhat of an utter noob at turing and there is this one question in my practice questions that I can't seem to grasp. Question: "Write a program to find the sum of a number of terms of the infinite series 1+x+x**2+x**3+x**4+.. where the number of terms n to be evaluated and the value of x are input before the summation begins. Experiment with different values of n and x." %Chapter 4 Exercise 9 %March 19 2008 Quote: var n:real
var x:real put "Please enter the number of terms that will be evaluated" get n put "Please enter the value of x" get x I can't seem to figure out the next thought process required inorder to solve this problem. I remember an attempt where I incorporated count and a few other options and came to a problem where I could not figure out how to add exponents to x that would be calculated in order to find the sum.Unfortunately, I have misplaced that file. Any help or suggestions welcome. Thanks |
Author: | Saad [ Wed Mar 19, 2008 6:40 pm ] |
Post subject: | Re: Any Help Appreciated |
Look up using for loops to go from 1 to n and adding each number to a variable. Futhermore the exponent function is **. Eg. sum := sum + 2 ** 2 is the same as adding 4 to sum |
Author: | DstSoldier [ Wed Mar 19, 2008 6:51 pm ] |
Post subject: | Re: Any Help Appreciated |
Saad @ Wed Mar 19, 2008 6:40 pm wrote: Look up using for loops to go from 1 to n and adding each number to a variable. Futhermore the exponent function is **.
Eg. sum := sum + 2 ** 2 is the same as adding 4 to sum Yes thanks for the feedback I knew exponents were double **, could you perhaps point me towards the thread or keyward needed in order to find how to use loops going from 1 to n. |
Author: | Saad [ Wed Mar 19, 2008 6:53 pm ] |
Post subject: | RE:Any Help Appreciated |
This is the place for "for loops" http://compsci.ca/v3/viewtopic.php?t=3678 Taken out of the Turing Walkthrough found Here |
Author: | DstSoldier [ Wed Mar 19, 2008 7:11 pm ] |
Post subject: | Re: Any Help Appreciated |
Quote: /* March 17th, 2008 Program designed to use inputted value of x and square it by the number of terms inputted, obtaining the sum. CH4EX9*/ var sum, x : real var no_of_terms : int put no_of_terms:0 put "Enter number of terms to input." get no_of_terms put no_of_terms, " terms are going to be entered." put "" put "Enter value of x." get x put x, " is the value of X." for i : 1.. no_of_terms put "1+x+", i end for I read the loops tutorial to reconfirm what I know I can't seem to get the program to run says no variable, any suggestions? |
Author: | Saad [ Wed Mar 19, 2008 7:12 pm ] |
Post subject: | Re: Any Help Appreciated |
[quote="DstSoldier @ Wed Mar 19, 2008 7:11 pm"] Quote: var no_of_terms : int put no_of_terms:0 no_of_terms was never initialized and as a result has no value. You want to give it a value ![]() |
Author: | DstSoldier [ Wed Mar 19, 2008 7:32 pm ] |
Post subject: | Re: Any Help Appreciated |
Quote: /* March 17th, 2008 Program designed to use inputted value of x and square it by the number of terms inputted, obtaining the sum. CH4EX9*/ var sum, x : real var no_of_terms : int put "Enter number of terms to input." get no_of_terms cls put no_of_terms, " terms are going to be entered." put "" put "Enter value of x." get x put x, " is the value of X." for i : 1.. no_of_terms put "1+x+", i %This is what I want to do before I end the program cant figure out how to to do it. sum=: i+x**no_of_terms end for hmm I seem to have fixed that last problem cant seem to get the sum be equal to i+x to the exponent to the no_of_terms. |