Thread: Quick question on geometric series'

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    4

    Smile Quick question on geometric series'

    How would one write the geometric series
    a + a r + a r2 + … + a rN-1

    as an expression in C?

    At the moment I have the code
    Code:
         sum = a * (pow(r, N - 1));
    where a is the first value in the series, N is the number of values in the series and r is the common ratio, but this only prints the last value of the series.

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    17
    Code:
    int i,n;
    for (i=0; i<n; i++)
        printf("%d\t", a * (pow(r,N-1)));

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @chongmet:
    Please don't give out solutions. It's against our policy and it robs the OP of the opportunity to solve the problem on their own and learn from their efforts. Furthermore, your posted code sample does not actually solve the OP's problem.

    @bevin:

    1. You will need to use loops. A for loop is a good loop to use here; they are good for counting loops, and you are basically counting through the possible values of the exponent.
    2. Start simple and build on that. Make sure you test the building blocks, don't build on a broken foundation. Start with a loop that just prints all the exponent values you will need to use. Then modify it to print ri where i is the exponent value in your loop. Then, print each term a*ri.
    3. The last step you need is to sum these values. Thus, you should have a sum (addition) operator in there somewhere.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You can also use the fact that ri is equal to r* ri-1 for i > 1.

    So if one iteration of the loop has computed rn, that value can be used directly to compute rn+1 in the next iteration .... if the value is saved between interations.


    Using pow() in such circumstances to compute everything from scratch is for lazy people who can't think through the logic of loops.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    4

    Smile

    Thanks all, I have now got the following for loop which prints the correct values:
    Code:
    for (i = 0; i < N; i++)
        {
            sum = sum + a*pow(r, i);
        }
    @grumpy any ideas on what to use instead of pow()?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bevin
    any ideas on what to use instead of pow()?
    Observe that for each term, the r component is r times the previous term's r component.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by bevin View Post
    @grumpy any ideas on what to use instead of pow()?
    Read my previous post. I already answered that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    Quote Originally Posted by grumpy View Post
    Read my previous post. I already answered that.
    Indeed you did Thank you!

  9. #9
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    I've got the loop to work without the use of the pow() function. Thank you all!
    Code:
        for (i = 0; i < N; i++)
        {
            sum1+=(a*t);
            t*=r;
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Geometric search/intersection problem
    By getsmart1 in forum Tech Board
    Replies: 9
    Last Post: 09-24-2012, 08:52 AM
  2. Arithmetic/Geometric/Harmonic
    By DJ_Steve in forum C Programming
    Replies: 12
    Last Post: 08-23-2009, 08:34 PM
  3. Program for average, geometric mean, harmonic mean
    By Northstar in forum C Programming
    Replies: 5
    Last Post: 11-07-2007, 11:33 AM
  4. Series Question
    By Raekwon1 in forum C Programming
    Replies: 5
    Last Post: 04-16-2007, 08:03 PM
  5. Geometric Libraries and Coordinate planes
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 11-11-2001, 02:46 PM