Write a C program to implement Trapezoidal method.

Algorithm:

Step 1.  Read x1, x2, e {x1 and x2 are the two end points of the internal the allowed error in integral is e}
Step 2.  h = x2 - x1
Step 3.  SI = (f(x1) + f(x2))/2;
Step 4.  I = h - si
Step 5.  i = 1 Repeat
Step 6.  x = x1 + h/2
Step 7.  for J= 1 to I do
Step 8.  SI = SI + f(x)
Step 9.  x = x + h
         End for

Step 10. i = 21
Step 11. h = h/2 {Note that the internal has been halved above and the number of points where the function has to be computed is doubled}
Step 12. i0 = i1
Step 13. i1 = h.si
Step 14. until / I1 - i0 / <= c./i1/
Step 15. Write I1, h, i
Step 16. Stop.

Flowchart:

Program:

#include<stdio.h>
#include<math.h>
main()
{
 float h, a, b, n, x[20], y[20], sum = 0, integral;
 int i;
 clrscr();
 printf("enter the value of a, b, n:");
 scanf("%f %f %f", &a, &b, &n);
 printf("enter the values of x:");
 for(i = 0; i <= (n-1); i++)
 {
  scanf("%f", &x[i]);
 }
 printf("\n enter the values of y:");
 for(i = 0; i <= (n-1); i++)
 {
  scanf("%f", &y[i]);
 }
 h = (b-a)/n;
 x[0] = a;
 for(i = 1; i <= n-1; i++)
 {
  x[i] = x[i-1] + h;
  sum = sum + 2 * y[i];
 }
 sum = sum + y[b];
 integral = sum * (h/2);
 printf("approximate integral value is: %f", integral);
 getch();
}

Input & Output:

enter the values of a, b, n
123
enter the values of x:
123
enter the values of y:
123
approximate integral value is 2.166667