Implementing Numerical Integration Using Function Pointers T
     Implementing Numerical Integration Using Function Pointers  The problem is to write a function \"integrate\" with prototype://FUNC represents functions of one variable that take a double as input and returns a double typedef double (*FUNC){double);  double integrate(FUNC f, double a, double b);  so that when it is passed a function f and bounds a and b, the call:  integrate(f, a, b) will return the value of the definite integral of f evaluated between a and b.  test integrate on the following three functions:  double line(double x){return x;  {double square(double x){return x*x;  {double cube(double x){return x*x*x;  {And the following main function:  int main(){cout 
  
  Solution
#include <stdio.h>
 #include <math.h>
 #define PI 3.14159265
double f(double x);
 double simpsons(int n, double a, double b);
int main()
 {
 double x0 = 0, xn = 10;
 int i, n = 20;
for (i = 1; i <= 2; i++) {
 n = i*20;
 printf( \"For n=%d, the integral is %f\ \ \", n, simpsons(n, x0, xn));
 n=n+20;
 }
 return 0;
 }
double f(double x)
 {
 double y;
y = (x*exp(x))*pow(sin(0.5*PI*x),2);
return y;
 }
double simpsons(int n, double a, double b)
 {
 int i;
 double dx, x, sum;
dx = (b - a) / n;
 sum = f(a) + f(b);
 for (i = 1; i < n; i++) {
 x = a + dx * i;
 sum += 2 * (1 + i%2) * f(x);
 }
 sum *= dx/3;
 return sum;
 }

