Hi guys. I'm pretty new to C program. I've been given this assignment to write a program that asks the user thow many terms of the serires equation to use in approximating PI. The formula given is pi=4X(1- 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - ....)

Here is the script that I've written but I'm stuck. Please advice. My alogarithm might be wrong

``````#include <stdio.h>
int main (void)
{
int x,
i;
//int sum=0;
double PI;
float sum=0, y;

printf("Please enter the terms of series that should be included> ");
scanf("%d", &x);

for (i=1; i<=x; i++){
i=y;

if (i%2==1) //odd

if (sum%2==0)
sum=sum+1/y;
else
sum=sum-1/y;
}

PI= 4*sum;
printf("The sum is %f\n", sum);
printf("Approximate value of PI is %f\n", PI);

return 0;
}``````

Hi,

First of all welcome to the world of computer programming.
Here is ur solution :

``````for (i=1; i<=x; i++)
{

if (i%2!=0)
sum=sum+1/y;
else
sum=sum-1/y;

y=y+2;
}``````

Initial value of sum and y should be 0 and 1 respectively. Rest of ur program was ok. I haven't compiled this code. I have just put logic. Please let me know if this code doesn't work.
:)

I could run it but the answer is wrong. I think i know where went wrong but i can't figure out the solution. The sum should be less than 1. But since the sum is declared as int. So it cannot take decimal places. So what should i change to convert sum from int to float?

Use double (not float) type for math calculations!

``````double x, y, z, sum, pi;
...
sum = 1.0;
for (i = 0, y = 3.0; i < n; ++i, y += 2.0)
{
z = 1.0 / y;
if ((i & 1) == 0)
sum -= z;
else
sum += z;
}
pi = 4.0 * sum;``````
commented: doubles yes! +17

Maybe you need to do something like:

1.0/y;

Thank you guys. I've manage to work it out. Here is the code:

``````#include <stdio.h>
int main (void)
{
int x;
float sum=0;
double PI;

printf("Please enter the terms of series that should be included> ");
scanf("%d", &x);

for (int i=1; i<x; i+=2)
if (i%4==1)
sum=sum+1./(double)i;
else
sum=sum-1./(double)i;

PI= 4*sum;
printf("The sum is %f\n", sum);
printf("Approximate value of PI is %f\n", PI);

return 0;
}
``````

I would change the sum to a `double` as well.

The `int i` needs to be declared outside the for loop I believe is it C99 mode or something.

And to increase the print precision you could do `%.9f`

I think the int declared inside the loops is also fine. I ran this program and everything works accordingly. Btw, what are the differences between float and double?

Well I compiled the following program and got the following errors:

foo.c

``````#include <stdio.h>
#include <string.h>

int main(void)
{

for ( int i = 0; i < 10; i++ ) /*int inside loop*/
{

}
return 0;
}``````

output

``````user@ubuntu804desktop:~\$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:7: error: ‘for’ loop initial declaration used outside C99 mode``````

I believe you can suppress this error if you compile with the necessary flags. -sdt=c99

> double or float?
http://www.gidforums.com/t-2934.html

If you want to be cryptic...

``````for (i = 0, y = 1.0, sum = 0.0, sign = 1.0 ;
i < n;
++i, sum += sign*(1/y), sign *= -1.0, y += 2.0);``````

*hides*

Declaring variables inside for loop initialization sections is indeed C99-only. I wouldn't do it if I were you.

Btw, what are the differences between float and double?

double just has more precision (usually). Oftentimes, a float is 4 bytes and a double is 8. There used to be a speed penalty for using floating-point numbers, which is why float exists, but there's not really any reason not to use double any more.

``for (i = 0, sum = 0.0; i < n; ++i, sum += ((i % 2) * 2 - 1) * (1.0 / (1 + i * 2)));``

*hides even further away* [/edit]

>*hides even further away*
There's nothing hidden there. Obfuscated, perhaps. Quite often, hard to read code, it is associated with cleverness, when the opposite it is true.

An abstraction layer is the only place where "hiding" some "goring details" of a program should be used. And interesting enough, it is for the sake of simplicity.

I tried DWKS line as the for statement. I am still seeing a 0 as the answer. My code reads:

``````#include <stdio.h>
#include <math.h>
int main()
{
int i,n; double neg,pi,sum;
pi=0;{
for (i = 0, sum = 0.0; i <= n; i++, sum + ((i/2) * 2 - 1) * (1.0 /
(1 + i * 2)));}
printf("The approximation of pi is %lf",pi);
return 0;
}
``````

Salem, are you online?

Welcome to the forum!