So, for an assignment in C++, I need to write a program which calculates the area under the curve of a certain input for a, b, and n values. I have the math formula down, but the assignment has a catch. I need to define the f() function separate from the main. I thought I had this written out correctly, and the compiler found no problems, but when I try running it, I get a message saying the code was not compiled, even though I did compile it. Does anyone know what the problem here is?
Update:
I've solved the problem with the compiler, but now I have a new problem. When I type in the values the trapazoidal method's output gets more out of sync with the other answers. Do I have the formula wrong?
#include <fstream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdlib.h>
double f(double);
usingnamespace std;
int main()
{
ofstream output("c:result.dat");
double x, a, b, n, i, delx, area;
cout<<"Enter values for a, b, and n.";
cin>>a>>b>>n;
area=0;
delx=(b-a)/n;
//Lower Limit
for(i=0; i<n; ++i)
{
area=area+f(a+i*delx)*delx;
}
cout<<"The area under the curve is "<<area<<"."<<endl;
output<<"The area under the curve is "<<area<<"."<<endl;
area=0;
//Upper Limit
for(i=1; i<=n; ++i)
{
area=area+f(a+i*delx)*delx;
}
cout<<"The area under the curve is "<<area<<"."<<endl;
output<<"The area under the curve is "<<area<<"."<<endl;
area=0;
//Midpoint Method
for(i=0; i<n; ++i)
{
area=area+f((a+0.5*delx)+i*delx)*delx;
}
cout<<"The area under the curve is "<<area<<"."<<endl;
output<<"The area under the curve is "<<area<<"."<<endl;
area=0;
//Trapazoidal Method
for(i=0; i<n; ++i)
{
area=area+f((a+i*delx)+(a+(i+1))*delx)*0.5*delx;
}
cout<<"The area under the curve is "<<area<<"."<<endl;
output<<"The area under the curve is "<<area<<"."<<endl;
return 0;
}
double f(double x)
{
return (0.6*pow(x,2)+2*x+3);
}
$ g++ int.cc
$ ./a.out
Enter values for a, b, and n.1 2 10
The area under the curve is 7.211.
The area under the curve is 7.591.
The area under the curve is 7.3995.