1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <iostream>
#include <cmath>
using namespace std;
void user_input( double &a, double &b, double &n )
{
cout << "Enter lower bounds: ";
cin >> a;
cout << "Enter upper bounds: ";
cin >> b;
cout << "Enter number of rectangles: ";
cin >> n;
}
double iterative( double &a, double &b, double &n )
{
double f = 0.0, area = 0.0;
double width = ( (b-a)/n );
while ( a < b )
{
f = ( pow(a, 5) + 10 );
area += ( f*width );
a += width;
}
return area;
}
double recursive( double &a, double &b, double &n )
{
double area = 0.0;
double f = ( pow(a, 5) + 10 );
double width = ( (b-a)/n );
if ( a >= b )
return 0;
else
{
area = f*width;
return (area + recursive(a+width, b, width));
}
}
int main()
{
int it_or_rec;
double a, b, n, f, width, area;
cout << "How would you like to integrate, iterative (1) or recursive (2): ";
cin >> it_or_rec;
cout << endl;
user_input(a, b, n);
if ( it_or_rec == 1 )
cout << iterative( a, b, n ) << endl;
else if ( it_or_rec == 2 )
cout << recursive( a, b, n ) << endl;
else
cout << "Error, restart program and try again." << endl;
return 0;
}
|