Thread: need help with series-- please

  1. #16
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    To start off, you need something to hold the sum, you need something to hold the current value of the denominator, and you need something to store the current sign. Taking all of that gets us:
    Code:
    double sum = 0.0;
    double denominator = 1.0;
    double sign = 1.0;
    Now you also need your loop:
    Code:
    for( int i = 0; i < 20; ++i )
    {
        //  What goes here?
    }
    In the body of the loop you need to increment the sum by the current term (sign multiplied by 4.0 divided by the value of the denominator). Then, you need to increment the denominator by 2, and multiply the sign by -1 so that it flips from positive to negative on each loop iteration. You also need to print the current value of the sum.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    thanks again
    hk_mp5kpdw

    look at what i did so far

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    
    double j=0;
    double pi=4;
    
    
    for (i=3; i < 20; i = i + 2)
    
    
    if (j&#37;2==0)
    {
    pi= pi - 4. /i;
    }
    else ( pi= pi+4. /i;
    )
    cout <<pi<<endl;
    return 0;
    }
    what's my problem

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > what's my problem
    Does it run?
    Does it produce the expected output?

    > if (j&#37;2==0)
    But all your j values are odd, since you start at 3 and add 2 each time.
    You need some other method for alternating the + and -
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sine series summation
    By kashya in forum C Programming
    Replies: 3
    Last Post: 12-17-2008, 08:00 PM
  2. Cosine - Maclaurin Series
    By carrotcake1029 in forum C Programming
    Replies: 12
    Last Post: 12-07-2008, 12:20 PM
  3. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  4. Problem with implementing Liebniz series
    By C_Necessity in forum C Programming
    Replies: 6
    Last Post: 06-15-2005, 12:39 PM
  5. array of series
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 06-22-2004, 01:06 PM