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

/* The function that we will be integrating. */

float integrand(float t) {
  float V0 = 5.0;
  float K = 0.2;
  return V0 * exp(-K * t);
}


/* Computes the trapezoidal approximation to the integral of integrand, over
   the range (a,b), using N subintervals. */

float trapezoid (float a, float b, int N) {

  float sum, h;
  int i;

  sum = integrand(a)/2;
  h = (b-a) / N;

  for (i = 1; i < N; i = i+1) {
    sum = sum + integrand(a + i*h);
  }

  sum = sum + integrand(b)/2;
  return(sum*h);

}


/* Inputs a value for N and integrates integrand between 0 and 5. */

void main () {

  int N;
  float approx;

  printf("Enter N: ");
  scanf("%d", &N);

  approx = trapezoid(0, 5, N);

  printf("Trapezoidal approximation for N = %d is %g\n", N, approx);

}

