/*
 * Parallelizing for MPI Lab
 * Karp Example
 * karp.c
 * Last revised RYL 2/6/95
 */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#define f(x) ((double)(4.0/(1.0+x*x)))
#define pi ((double)(4.0*atan(1.0)))

void startup (void);
int solicit (void);
void collect (double sum);

int main()
     
{
  /* 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+[(1/N)*(k-1/2)]**2) and then
   * multiplying the sum by (1/N). (This numerical integration rule
   * is called "Midpoint rule" and can be found in most numerical
   * analysis text books) The only input data 
   * required is N.                                       
   */
  double sum, w;
  int i, N;

  /*
   * The startup routine will create parallel tasks 
   */
  /* startup(); */

  /* 
   * The solicit routine will get and propagate the value of N
   */
  N = solicit();

  while (N > 0) {
    w = 1.0/(double)N;
    sum = 0.0;
    for (i = 1; i <= N; i++)
      sum = sum + f(((double)i-0.5)*w);
    sum = sum * w;
    /*
     * The collect routine will collect and print results
     */
    collect (sum);
    N = solicit ();
  }

  return (0);
}

/*  --------------------------------------------------------------  */
void startup (void)
{
}

/*  --------------------------------------------------------------  */
int solicit (void)
{
  int N;
  printf ("Enter number of approximation intervals:(0 to exit)\n");
  scanf("%d",&N);
  return (N);
}

/*  --------------------------------------------------------------  */
void collect(double sum)
{
  double err;
  err = sum - pi;
  printf("sum, err = %7.5f, %10e\n", sum, err);
}