/*----------------------------------------------------------------*/ /* Program chapter4_8 */ /* */ /* This program estimates the area under a given curve using */ /* trapezoids with equal bases. */ #include #include int main(void) { /* Declare variables and function prototypes. */ int num_trapezoids; double a, b, area; double integrate(double a, double b, int n); /* Get input from user. */ printf("Enter the interval enpoints, a and b\n"); scanf("%lf %lf", &a, &b); printf("Enter the number of trapezoids\n"); scanf("%d", &num_trapezoids); /* Estimate area under the curve of 4e^-x */ area = integrate(a, b, num_trapezoids); /* Print result. */ printf("Using %d trapezoids, the estimated area is %f\n", num_trapezoids, area); return 0; } double integrate(double a, double b, int n) { double sum = 0, x, base, area; double f(double x); int k; base = (b-a)/n; for(k=2; k<=n; k++) { x = a + base*(k-1); sum = sum + f(x); } area = 0.5*base*(f(a) + 2*sum + f(b)); return area; } double f(double x) { return(4*exp(-x)); }