/* Problem 1--Spheres
   It turns out there is a fairly nice formula for computing the surface
   of an n-dimensional sphere.
   S = 2 * Product(i=0,n-2;Int[-PI/2,PI/2]cos^i (x) dx) r^(n-1).
   The content is just the antiderivative of the surface with respect
   to r.  I used Simpson's rule to evaluate the integrals, though they
   can all be done in closed form.  A Monte Carlo method might also work
   to compute the content of the spheres. */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define Pi 3.1415926535897932384626433832795

int main (int argc, char **argv);
double IntCos (int n);

int main (int argc, char **argv) {

  double S[101], V[101];
  FILE *in, *out;
  int i, cs;

 cs = 0;
 in = fopen ("prob1.in","r");
 out = fopen ("prob1.out","w");
 S[0] = 0; V[0] = 1; /* Compute the volume for all of them */
 S[1] = 2; V[1] = 2;
 for (i=2; i <= 100; i++) {
  S[i] = S[i-1]*IntCos (i-2);
  V[i] = S[i]/i;
 }
 while (fscanf (in,"%d",&i),!feof (in)) /* read each case */
  fprintf (out,"Case %d: "
          "Content is %.3f r^%d and surface is %.3f r^%d.\n",
          ++cs,V[i],i,S[i],i-1);
 fclose (in);
 fclose (out);
 return EXIT_SUCCESS;
}

/* Computes the Integral from -PI/2 to PI/2 of cos^n (x)dx using
   Simpson's rule. */
double IntCos (int n) {

  int i;
  double c, sum;

 sum = 0;
 for (i=0; i <= 1000; i++) {
  c = pow (cos(-Pi/2+i*Pi/1000),n);
  if (i==0 || i==1000) sum += c;
  else if (i%2==0) sum += 2*c;
  else sum += 4*c;
 }
 return sum*Pi/3000;
}
