/*
 * Integ.c by Richard J. Davies
 * from `Introductory Java for Scientists and Engineers'
 * chapter: `Java for C Programmers'
 * section: `Similarities'
 *
 * This program integrates 1/(1+x) between x=1 and x=2 using the trapezium
 * rule. It is provided for comparision with Integ.java which is exactly the
 * same program written in the Java language.
 */

#include <stdio.h>

int main(int argc, char* argv[])
{
  double total, x, y;
  int i;

  total = 0;

  // We are using the trapezium rule, so each end
  // point has a weighting of one.

  total += 1 / (1 + 1.0);  // LH end, x = 1
  total += 1 / (1 + 2.0);  // RH end, x = 2

  // whilst each middle point has weight two
    
  for (i=1; i<=99; i++)
  {
    x = 1 + i/100.0;
    y = 1 / (x + 1);

    total += 2 * y;
  }

  // Finally, we multiply by half the step size
	
  total *= 0.005;

  printf("%.16f\n", total);
}

