# PI Equation in C

• 10-22-2006
wallysworld
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!
• 10-22-2006
quzah
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.
• 10-23-2006
SKeane
Don't forget to NOT start your "simple" loop counter at zero.
• 10-23-2006
wallysworld
"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.
• 10-23-2006
wallysworld
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)++; }```
• 10-23-2006
Salem
> it will compile
What with?
I see several glaring syntax errors.

> (*iterations)++
Missing ;

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.
• 10-23-2006
wallysworld
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.
• 10-23-2006
swoopy
>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.
• 10-23-2006
wallysworld
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-23-2006
ssharish2005
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
• 10-23-2006
ssharish2005
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
• 10-23-2006
swoopy
> 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));`
• 10-23-2006
wallysworld
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));`
?
• 10-23-2006
swoopy
>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));`