//
// purpose : Routine to integrate arbitrary functions
// author :  Hans Bloedmann
// version : 0.01
//
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

double Integral(double f, double a, double b, int N)  // Routine to integrate 
{                                                     // an arbitrary function f(x)
     double h = (b-a)/N;                              // with Simpsons formula.
     int factor = 2;                                  // a = lower limit
     double x = a;                                    // b = upper limit                          
     double value = f(a);                             // N = subdivisions
     for(int i=1; i<= N-1; i++) {                     //
        if(factor == 2) factor = 4;                   // Was ist falsch
        else factor = 2;                              // in diesem Programm ???
        x += h;
        value += f(x)*factor;
     }
     value += f(b);
     value *= h/3;
     return value;                          
}

double my_function(double x) {
   return exp(x)*cos(x);
}

int main()
{
  double a = 0.;             
  double b = 3.1415927;                
  int N = 100;
  double y1 = Integral(cos,a,b,N);                  // cos function from math.h
  double y2 = Integral(sin,a,b,N);                  // sin function from math.h
  double y3 = Integral(my_function,a,b,N);          // my own function

  cout << " Integral from " << a << " to " << b << " over function " << endl;
  cout << " cos(x) " << y1 << " sin(x) " << y2 << " my_function(x) " << y3 << endl;

  return 0;
}





