
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define f(x) x*x

double a = 0, b = 2.0;

void main( int argc, char** argv )
{
  int n, i, j, incr;
  double* res, x, y, sum;
  double L = b - a;

  if( argc < 4 ) exit(-1);
  n = atoi(argv[1]);
  incr = atoi(argv[2]);
  
  res = (double*) malloc( (n+1)*sizeof(double) );
    
  for( j=1; j<=n; j+=incr )
    {
      sum = 0.0;
      for( i=0; i<j; i++ )
	{
	  switch( argv[3][0] )
	    {
	    case 'u': /* Uniform sampling */
	      
	      x = a + drand48()*L;
	      sum += f(x)*L;
	      break;

	    case 'i': /* Importance sampling */
	      
	      x = a + sqrt(drand48())*L;
	      sum += x*2;
	      break;

	    case 's': /* Stratified sampling */
	      x = a + (i + drand48())*L/j;
	      sum += f(x)*L;
	      break;

	    case 'm': /* Stratified importance sampling */
	      x = a + sqrt((i + drand48())/j)*L;
	      sum += x*2;
	      break;
	      
	    default:
	      exit(-1);
	    }
	}
      res[j] = sum/j;
    }

  for( j=1; j<=n; j+=incr )
    printf("%8.20e\n", res[j]);
  
  free(res);
}





