#include #include #include #define f(x) ((float)(4.0/(1.0+x*x))) #define pi ((float)(4.0*atan(1.0))) main(int argc, char * argv[]) { /* This simple program approximates pi by computing pi = integral * from 0 to 1 of 4/(1+x*x)dx which is approximated by sum * from k=1 to N of 4 / ((1 + (k-1/2)**2 ). The only input data * required is N. */ /* Each process is given a chunk of the interval to do. */ float err, sum, w; int i, N; printf ("Enter number of approximation intervals:(0 to exit)\n"); scanf("%d",&N); while (N > 0) { /* calcualte step width, initialize sum */ w = 1.0/(float)N; sum = 0.0; for (i = 0; i < N; i++) sum = sum + f(((float)i-0.5)*w); sum = sum * w; err = sum - pi; printf("sum = %7.5f, err = %10e\n",sum, err); printf ("Enter number of approximation intervals:(0 to exit)\n"); scanf("%d",&N); } /* while (N > 0) */ }