PI Equation in C

This is a discussion on PI Equation in C within the C Programming forums, part of the General Programming Boards category; hi, i'm currently working on a program which asks the user to input an integer value of "n" to determine ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    PI Equation in C

    hi,

    i'm currently working on a program which asks the user to input an integer value of "n" to determine the number of iterations of the denominator in the equation for PI listed below.

    I have all the basic input, output, function calls etc; however, i'm a bit confused on how to turn the equation below into a c expression with variable input.

    PI = 4(1 + 1/3 - 1/5 + 1/7 etc... (where int n is the value of the denominator to continue to)
    PI = 4 + 4/3 - 4/5 + 1/7 (just another way to look at it)

    anyone able to help? Thanks a lot!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So in your example, what is N? 3? 7? It's a pretty simple problem. Start a loop counter. Increment your divisor by 2 each time. Stop when your loop counter is N. (Or when your divisor is, however you're doing whatever you're doing.)


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Don't forget to NOT start your "simple" loop counter at zero.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    "N" is the value of the denominator that i'm continuing to, its input by the user.

    i think i've got the for loop now, i guess the only think i'm still confused on is exactly how to incorporate that equation into my for loop, if that makes sense.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    so here is what i've come up with...
    it seems to me like it should execute correctly, but somewhere something is causing my output to be bogus large numbers; however, it will compile... does anyone see my error, i can't figure it out...

    Code:
    #include<stdio.h>
    
    void pi(int n, double *pi_value, int *iterations); //declare function
    
    int main (void)
    {
        int n;                    //number of terms
        int iterations = 0;        //number of actual loop iterations in pi
        double pi_value = 0;   //computed value of pi
        
        printf("Please enter an odd integer n;  ")'
        printf("quit with a non-positive or even integer:\n");
    
       /*-- Read n and display pi.  Quit with a non-positive n.  --*/
       scanf("%d", &n);
       while(n>0 && n%2)
       {
           pi(n, &pi_value, &iterations);
           printf("%17d:   %0.81f with %d iterations\n", n, pi_value, iterations);
           scanf("%d", &n);
       }
       
       return(0);
    }  
    
    void pi(int n, double *pi_value, int *iterations)
    {
        int i;
        int ADD = 1 // 1 = true & 2 = false
       
        for(i=0; i<n; i++)
        {
            switch(ADD) 
            {
               case 1:
                 *pi_value += (4/((n*2)-1));
                 ADD = 2;
                 break;
               case 2:
                 *pi_value -= (4/((n*2)-1));
                  ADD = 1;
             {   break;
        }
        (*iterations)++;
    }
    Last edited by wallysworld; 10-23-2006 at 04:29 PM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > it will compile
    What with?
    I see several glaring syntax errors.

    > (*iterations)++
    Missing ;

    > for(ADD == 2)
    Looks more like if() rather than for()
    But break isn't meaningful inside an if

    > pi_value -= (4/((n*2)-1));
    pi_value was a pointer, so perhaps
    *pi_value -= (4/((n*2)-1));

    > int iterations; //number of actual loop iterations in pi
    > double pi_value; //computed value of pi
    Try initialising them to something, say zero.
    Rather than just adding values to junk, and getting junk back as the result.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    sorry salem, the "glaring" syntax errors were typos when i was typing that in, glance at it again if you don't mind and see if you catch anything now, i also edited the values of iterations and pi_value, as well as adding the pointers you noticed... thanks for the help.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >glance at it again if you don't mind and see if you catch anything now
    You should cut and paste your latest code. Otherwise it's a guessing game.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    sorry, i wasn't clear, i meant i edited it in my previous post, but here it is again

    Code:
    #include<stdio.h>
    
    void pi(int n, double *pi_value, int *iterations); //declare function
    
    int main (void)
    {
        int n;                    //number of terms
        int iterations = 0;        //number of actual loop iterations in pi
        double pi_value = 0;   //computed value of pi
        
        printf("Please enter an odd integer n;  ")'
        printf("quit with a non-positive or even integer:\n");
    
       /*-- Read n and display pi.  Quit with a non-positive n.  --*/
       scanf("%d", &n);
       while(n>0 && n%2)
       {
           pi(n, &pi_value, &iterations);
           printf("%17d:   %0.81f with %d iterations\n", n, pi_value, iterations);
           scanf("%d", &n);
       }
       
       return(0);
    }  
    
    void pi(int n, double *pi_value, int *iterations)
    {
        int i;
        int ADD = 1 // 1 = true & 2 = false
       
        for(i=0; i<n; i++)
        {
            switch(ADD) 
            {
               case 1:
                 *pi_value += (4/((n*2)-1));
                 ADD = 2;
                 break;
               case 2:
                 *pi_value -= (4/((n*2)-1));
                  ADD = 1;
             {   break;
        }
        (*iterations)++;
    }

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    (4/((n*2)-1));
    isn't it your denominator should be incremented by 2 every time rather than multiplying it by 2 and sub

    ssharish2005

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    switch(ADD) 
            {
               case 1:
                 *pi_value += (4/((n*2)-1));
                 ADD = 2;
                 break;
               case 2:
                 *pi_value -= (4/((n*2)-1));
                  ADD = 1;
             {   break;
    ssharish2005

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for(i=0; i<n; i++)
    I believe you want the loop from 1 to n:
    Code:
        for(i=1; i<=n; i++)
    > *pi_value += (4/((n*2)-1));
    This will do int arithmetic, unless you tell it otherwise:
    Code:
                 *pi_value += (4.0/((n*2)-1));

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    okay, thanks swoopy...

    i changed the 1 to n as you suggested, and you're right, i don't want int arithmetic... would you specify double by...

    Code:
    (double)*pi_value += (4.0/((n*2)-1));
    ?

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >would you specify double by...
    wallysworld pi_value is already a pointer to a double, so you just need to be sure it does double arithmetic on the right hand side. You can do that by making the constants double:
    Code:
                 *pi_value += (4.0/((n*2.0)-1.0));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Volume of a Cone Equation always equals 0
    By Devolution in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 02:13 AM
  2. Pi - Hm, somethign is not right here.
    By MadnessRed in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2008, 01:07 PM
  3. Replies: 15
    Last Post: 11-04-2006, 03:06 AM
  4. IDEA: Equation solver
    By Magos in forum Contests Board
    Replies: 2
    Last Post: 01-07-2003, 10:46 AM
  5. C for PI
    By Lynux-Penguin in forum C Programming
    Replies: 13
    Last Post: 04-28-2002, 07:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21