//****************************************************************** // 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 #include // For setprecision() using namespace std; 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 << fixed << showpoint; // Set up floating pt. // 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++) { area = area + RectArea(leftEdge, width); leftEdge = leftEdge + width; } // Print result cout << "The result is equal to " << setprecision(7) << area << endl; return 0; } //****************************************************************** void GetData( /* out */ float& low, // Bottom of interval /* out */ float& high, // Top of interval /* out */ int& divisions ) // Division factor // Prompts for the input of low, high, and divisions values // and returns the three values after echo printing them // Postcondition: // All parameters (low, high, and divisions) // have been prompted for, input, and echo printed { cout << "Enter low and high values of desired interval" << " (floating point)." << endl; cin >> low >> high; cout << "Enter the number of divisions to be used (integer)." << endl; cin >> divisions; cout << "The area is computed over the interval " << setprecision(7) << low << endl << "to " << high << " with " << divisions << " subdivisions of the interval." << endl; } //****************************************************************** float RectArea( /* in */ float leftEdge, // Left edge point of // rectangle /* in */ float width ) // Width of rectangle // Computes the area of a rectangle that starts at leftEdge and is // "width" units wide. The rectangle's height is given by the value // computed by Funct at the horizontal midpoint of the rectangle // Precondition: // leftEdge and width are assigned // Postcondition: // Function value == area of specified rectangle { return Funct(leftEdge + width / 2.0) * width; } //****************************************************************** float Funct( /* in */ float x ) // Value to be cubed // Computes x cubed. You may replace this function with any // single-variable function // Precondition: // The absolute value of x cubed does not exceed the // machine's maximum float value // Postcondition: // Function value == x cubed { return x * x * x; }