### Help with Error in Function?

I have a function written to calculate an integral using rectangles. I get this error: 'cannot convert double to double (*) (double) in assignment'.
But whenever I remove one of the doubles something is undeclared. Can anyone tell me what I need to fix?

double rect_integral(double a, double b, int n, int f)
{

double x;
double (* fx) (double);

double func_1 = 5*(pow(x,4))+3*(pow(x,2))-10*(x)+2;
double func_2 = pow(x,2)-10;
double func_3 = (40*x)+5;
double func_4 = pow(x,3);
double func_5 = 20*(pow(x,2))+(10*x)-2;

switch (f)
{
case 1: fx = func_1; break;
case 2: fx = func_2; break;
case 3: fx = func_3; break;
case 4: fx = func_4; break;
case 5: fx = func_5; break;
}
double height;
double total_area;
double width;
double area;

width = (b-a)/n;
total_area=0;
x = a;

for (int i=1; i <= n; i++) {
x+=width;
total_area+=width*fx(x);
}
}

`double (* fx) (double);` = function pointer.

variables of type double:
 ``12345`` ``````double func_1 = 5*(pow(x,4))+3*(pow(x,2))-10*(x)+2; double func_2 = pow(x,2)-10; double func_3 = (40*x)+5; double func_4 = pow(x,3); double func_5 = 20*(pow(x,2))+(10*x)-2;``````

attempting to assign variables of type double to a function pointer:
 ``12345`` ``````case 1: fx = func_1; break; case 2: fx = func_2; break; case 3: fx = func_3; break; case 4: fx = func_4; break; case 5: fx = func_5; break;``````
I still don't understand which part is wrong?
doubles are not functions? What's not to understand?

If you have a compiler with lambda support you could do
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include #include double rect_integral(double a, double b, int n, int f) { using std::pow ; std::function fx ; auto func_1 = [](double x) { return 5*(pow(x,4))+3*(pow(x,2))-10*(x)+2; }; auto func_2 = [](double x) { return pow(x,2)-10; }; auto func_3 = [](double x) { return (40*x)+5; }; auto func_4 = [](double x) { return pow(x,3); }; auto func_5 = [](double x) { return 20*(pow(x,2))+(10*x)-2; }; switch (f) { case 1: fx = func_1; break; case 2: fx = func_2; break; case 3: fx = func_3; break; case 4: fx = func_4; break; case 5: fx = func_5; break; } double height; double total_area; double width; double area; width = (b-a)/n; total_area=0; double x = a; for (int i=1; i <= n; i++) { x+=width; total_area+=width*fx(x); } return total_area; }``````

If not, make your functions... functions.
Topic archived. No new replies allowed.