Edit: I changed the code more and got this:
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
|
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
double func(double x){
return (5.0*(pow(x,3.0))+7.0*(pow(x,2.0))-3.0*x+4.0);
}
int main(){
double a, b;
double x = 0;
double w, y=func(x);
double area;
cout << "What is the value of a?: ";
cin >> a;
cout << "What is the value of b?: ";
cin >> b;
if(a > 0 && b > 0 && a <= b){
w = (b-a)/10;
x = a;
while(x >= a && x < b){
y = func(x);
x += w;
area += (w*y);
cout << "The area under the curve is: " << area << endl;
}
}else if(a < 0 || b < 0){
perror ("\nInvalid data, either a or b < 0\n");
}else{
perror("\nInvalid data, a > b");
}
return 0;
}
|
Which now gives me this as an output:
What is the value of a?: 2
What is the value of b?: 7
The area under the curve is: 33
The area under the curve is: 92.1875
The area under the curve is: 188.688
The area under the curve is: 335.5
The area under the curve is: 547.5
The area under the curve is: 841.438
The area under the curve is: 1235.94
The area under the curve is: 1751.5
The area under the curve is: 2410.5
The area under the curve is: 3237.19
Which is much closer than I had before. Please, could someone help me make the last few changes to this so it works properly? According to my notes, the final answer here should be 3715.42