Thread: need help with series-- please

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    9

    need help with series-- please

    hey guys i need ur help to do this question please

    Calculate the value of pi = 4- 4/3+ 4/5 – 4/7 + 4/9 – 4/11 + ……………..

    Print a table that shows the approximate value of pi after each of the first 20 terms of this series.

    thanks

    i dont want the answer but i want to know how to do it.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I would suggest using a for loop.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    thank u salem
    i know that i have to use for loop
    my problem is how????????????????

    thanks again

  4. #4
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    Ignore the man before me. The while loop is superior in every way.
    .sect signature

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Quote Originally Posted by ggs View Post
    Ignore the man before me. The while loop is superior in every way.
    Considering the for loop is a while loop condensed into one statement what you said doesnt really make any sense. And Salem is a very seasoned proffesional so he was is correct 99% of the time. Although either loop would work well.
    Double Helix STL

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    thanks ggs
    but now i need to know it's work
    i mean how can i do my program
    thanks again

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well you want 20 terms, so how about
    for ( nTerms = 0 ; nTerms < 20 ; nTerms++ )

    Don't think we're being mean or anything, but it's hard to describe what is basically 1 line of code without giving the whole game away.

    Post an effort of your own.
    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.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    thank u all
    here what i did

    pi=pi = 4- 4/3+ 4/5 – 4/7 + 4/9 – 4/11 + …………….
    i= 3,5,7,9,11,................
    for (i=3; i < 20; i = i + 2)
    j=0
    pi=4
    if (j&#37;2==0)
    pi= pi - 4. /i;
    else pi= pi+4. /i;
    cout <<pi<<endl;

    please let me know if what i did is correct or not

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The first term is 4 (or 4/1).
    The second term is -4/3.
    The third term is 4/5.
    The fourth term is -4/7.

    Do you see what is happening? The denominator increases by 2 every term and the sign flips every term. The sign flip can be done with a multiplication of -1 and the other, well... if you don't know how to add 2 to something you're in the wrong business. The rest is straight addition of those terms.
    Last edited by hk_mp5kpdw; 09-22-2007 at 09:30 PM. Reason: third <> fourth
    "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

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    thanks hk_mp5kpdw
    but if u would explain to me more that will be helpful
    please

  11. #11
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Now now. "Explain to me more." Consider what it is you are having trouble understanding (this might take some thought, maybe a snack or a quick nap), and then ask a question. Use messages to contact board members, not posts.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    CodeMonkey
    do u know how to do it????

  13. #13
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Anyone who read hk's post knows how to do it. Except you.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    so what do u want now
    is there any problem if i want to learn how to do it?
    people are diffreent.

    if u dont want to help please dont bother yourself with my problem

  15. #15
    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

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