|
|
|
|
|
|
|
//******************************************************************
// Area program
// This program finds the area under the curve of a mathematical
// function in a specified interval. Input consists of two float
// values and one int. The first two are the low, high values for
// the interval. The third is the number of slices to be used in
// approximating the area. As written, this program finds the
// area under the curve of the function x cubed; however, any
// single-variable function may be substituted for the function
// named Funct
//******************************************************************
#include <iostream.h>
#include <iomanip.h> // For setprecision()
float Funct( float );
void GetData( float&, float&, int& );
float RectArea( float, float );
int main()
{
float low; // Lowest value in the desired interval
float high; // Highest value in the desired interval
float width; // Computed width of a rectangular slice
float leftEdge; // Left edge point in a rectangular slice
float area; // Total area under the curve
int divisions; // Number of slices to divide the interval by
int count; // Loop control variable
cout.setf(ios::fixed, ios::floatfield); // Set up floating pt.
cout.setf(ios::showpoint); // output format
GetData(low, high, divisions);
width = (high - low) / float(divisions);
area = 0.0;
leftEdge = low;
// Calculate and sum areas of slices
for (count = 1; count <= divisions; count++)
{
// Invariant (prior to test):
// area == sum of areas of first count-1 rectangles
// && leftEdge == low + (count-1) *width
// && 1 <= count <= divisions+1
area = area + RectArea(leftEdge, width);
leftEdge = leftEdge + width;
}
|
|
|
|
|
|