Hey guys, my first post so forgive me if I balls up posting this code. Anyway, this is my program for calculating the indefinite integral of any programmer-defined function given two limits of integration. Any thoughts or comments would be appreciated!
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
bool even(int x) //simple even test. if even, returns 1
{
if (x%2==0)
return 1;
else
return 0;
}
double f(double x) //here function of curve is defined by programmer
{
//here programmer defines function of curve
//const long double e = 2.718281828459045235360287471352662497757247093;
return (pow(x,3));
}
int choose_coef(int i, int n) //these coefficients are based on Simpon's Rule
{
if (i==0 || i==n) //first and last time around
return 1;
else if (!(even(i))) //if i is odd
return 4;
else
return 2;
}
int main()
{
double a=0;
double b=0;
int p=0;
int n=0;
double dx=0;
char ans;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(10);
do {
system("CLS");
cout << "Please set limits of integration separated by a space: ";
cin >> a >> b;
cout << "Please set number of parabolas to estimate with: ";
cin >> p;
cout << endl;
cout << "Range of integration will be divided into " << p*2 << " divisions (n=" << p*2 << "). \nIntegral will be"
<< " estimated with " << p << " parabolas." << endl << endl;
system("PAUSE");
n=2*p; //two divisions for each parabola
dx = (b-a)/n; //dx is length of each division
int coef=0;
double series=0;
cout << "Series looks like: " << endl;
for (int i=0; i<=n; i++)
{
coef = choose_coef(i,n);
series += coef*f(a+(dx*i));
cout << coef << " * " << "f(" << a+(dx*i) << ") +" << endl;
}
cout << "dx/3 = " << dx/3 << endl;
cout << "series = " << series << endl;
cout << endl << "Definite integral = " << (dx/3)*(series);
cout << endl << endl << "Repeat? y/n: ";
cin >> ans;
} while (ans == 'y' || ans == 'Y');
cout << endl << endl << endl;
return 0;
}