#include <stdio.h> /* for input/output */
#include <math.h> /* for maths */

float f(float x) { 
  return 5.0*pow(x,4.0) + 4.0*pow(x,3.0) + 3.0*pow(x,2.0) + 2.0*x + 1.0;
}

main() {
  float x1, xN; /* first and last terms */
  float I; /* integral result */
  float h; /* trapezium bar width */
  int N; /* number of terms */
  int i; /* term number */
  printf("Extended Trapezoidal Numeric Integration\nBy Ian Hickson\nRecompile code to change equation.\n");
  printf("\nEnter the low bound: ");
  scanf("%f", &x1);
  printf("\nFYI, the equation at the low bound is: f(%f)=%f.", x1, f(x1));
  printf("\nEnter the high bound: ");
  scanf("%f", &xN);
  printf("\nFYI, the equation at the high bound is: f(%f)=%f.", xN, f(xN));
  printf("\nEnter the number of terms: ");
  scanf("%d", &N);
  printf("\nWorking...");
  I = 0.5 * (f(x1)+f(xN)); /* First and last terms */
  h = (xN - x1) / (N - 1); /* Find the trapezium width */
  for (i=2; i<N; i++) {
    I += f(x1 + (i-1)*h);
  }
  I *= h;
  printf ("\nThe integral is approximately: %f\n", I);
}



/* OUTPUT:
none yet
 */

/* COMPILATION ERRORS 
none yet
 */
