Any idea where I am going wrong? We have to make a function integrate to work with the other functions given. I had it working before but I would only get all -4 as my answers but only the first one should be -4. can someone please provide me with what should more or less be put in my integrate function?
cout << "The integral of f(x)=x between 1 and 5 is: " << integrate(line,1,5) << endl;
cout << "The integral of f(x)=x^2 between 1 and 5 is: " << integrate(square,1,5) << endl;
cout << "The integral of f(x)=x^3 between 1 and 5 is: " << integrate(cube,1,5) << endl;
system("pause");
What you intended to do was somthing like the following. You either needed to put your functions before you are using them in your code or at least put some function declarations.
#include <iostream>
usingnamespace std;
typedefdouble (*FUNC)(double);
double integrate(FUNC f, double a, double b){
return f(b) - f(a);
}
double line(double x){
return x;
}
double square(double x){
return x*x;
}
double cube(double x){
return x*x*x;
}
int main(){
cout << "The integral of f(x)=x between 1 and 5 is: " << integrate(line,1,5) << endl;
cout << "The integral of f(x)=x^2 between 1 and 5 is: " << integrate(square,1,5) << endl;
cout << "The integral of f(x)=x^3 between 1 and 5 is: " << integrate(cube,1,5) << endl;
//system("pause");
}