Part A) Write a C++ program that calculates the area under a curve. Here is the
ID: 3682761 • Letter: P
Question
Part A)
Write a C++ program that calculates the area under a curve.
Here is the equation: f(x) = x^2 +1.5x +4
You must prompt the user for the beginning and the ending x values.
You are to assume, but not check, that the user will put in whole positive numbers.
The units are inches.
The program input/output should look something like this:
This program calculates the area under a curve between two points on the x axis.
The equation is: XXXXXXXX
Please Enter the min x value:
Please Enter in the max x value:
The area under the curve between ‘min x’ and ‘max x’ is: ‘total’ square inches.
You should calculate the area using the basic “rectangular slice” method. Set your x axis intervals at 1 inch. For example, if the user input the x range from 1 inch to 4 inches, you would have three x ranges: from 1 to 2, from 2 to 3 and 3 to 4 in which you calculate the area and make a sum of the total.
PART B)
Here is a second equation: f(x) = x^2 + 1.5x – 4
Program this equation in addition to the first.
PART C)
Prompt the user for how many segments the x axis should be divided into. Ie., if the user sets the x axis range from 3” to 8”, part A and B would divide it into five segments. Now you prompt the user for how many segments should be used and do the proper calculations.
Explanation / Answer
#include <iostream>
#include <math.h>
using namespace std;
float f1(int low,int up)
{
int i,total=0.0;
for(i=low;i<=up;i++)
{
total=total+(i*i+i*1.5+4);
}
return total;
}
float f2(int low,int up)
{
int i,total=0.0;
for(i=low;i<=up;i++)
{
total=total+(i*i+i*1.5-4);
}
return total;
}
int main()
{
int low,up;
cout << "This program calculates the area under a curve between two points on the x axis ";
cout<< " The FIRST equation is: f(x)=x^2 +1.5x +4";
cout<< " The SECOND equation is: f(x)=x^2 +1.5x -4";
cout << " Enter x's minimum value : ";
cin >> low;
cout << "Enter x's maximum value : ";
cin >> up;
cout << " Area in square inches for FIRST equation ";
cout << "The area under the curve between "<<low<<" and "<<up<<" is: "<<f1(low,up)<<" square inches. ";
cout << " Area in square inches for SECOND equation ";
cout << "The area under the curve between "<<low<<" and "<<up<<" is: "<<f2(low,up)<<" square inches. ";
return 0;
}