Thread: Estimating Pi Program?

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    1

    Estimating Pi Program?

    I can't get this program to work.
    I am essentially given this series to calculate pi.
    π/2 = 1 +1/3+1/32/5+1/32/53/7+...
    Here are the steps I was given:
    1. [ Set-up ] First term t = 1, Initial sum s = t, term counter n = 1
    2. [ Increment term counter ] n = n + 1
    3. [ Calculate next term ] New t is old t multiplied by (n-1)/(2n-1)
    4. [ Add term to sum ] Add t to s➄
    5. [ Are we close enough? ] If t is small we are done: π estimate = 2s
    6. [ Loop ] go to ➁ for next term

    This is what i have so far:

    Code:
    #include <stdio.h>
    
    int main()
    
    {// Program for estimate Pi to a given accuracy
    
    // Filename pi.c Src: JWW 2015
    
    int n;float term, small, sum, pi;
    
    printf("Welcome to the pi estimator program\n");
    
    printf( ... );
    
    scanf("%f",&small);
    
    n = 1;
    
    term = 1;
    
    sum = term;
    
    for (;;)
    
    {
    n++;
    
    term = t*(n-1)/(2n-1);
    
    sum = t+s;
    
    if (term <= small) break;
    }
    
    pi = 2 s;
    
    printf("Our pi estimate is %25.20f\n",pi);
    
    printf("This differs from true pi by %gf\n",
    
    pi-3.141592653589793238462643383279502884197169399375105820974944e0 );
    
    printf("The smallest term included was %g\n", ... );
    
    printf("The number of terms summed was %i\n", ... );}
    I'm really confused and just starting off. The ... is where something sould be added but im just not sure. Thanks alot for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 5. [ Are we close enough? ] If t is small we are done: π estimate = 2s
    In code terms, you would write 2 * s

    > printf("The smallest term included was %g\n", ... );
    You already managed to print pi
    Read the message in the print statement, then look at your code to see which variable is best described by it.

    All those ... sections look like a "fill in the blanks" exercise from your tutor.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Another fskcing waste of time cross-poster
    Estimating Pi Program? - C And C++ | Dream.In.Code
    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. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  2. Zlib / Gzip - Estimating Uncompressed Size in Advance
    By Brandon9000 in forum C Programming
    Replies: 9
    Last Post: 12-04-2012, 04:46 PM
  3. The code for estimating-Memory usage
    By rac1 in forum C Programming
    Replies: 8
    Last Post: 02-08-2012, 05:20 PM
  4. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  5. estimating time remaining
    By Compengineer in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2003, 09:38 AM

Tags for this Thread