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);
}
return total_area;
}

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

variables of type double:
1
2
3
4
5
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:
1
2
3
4
5
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
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
37
38
#include <functional>
#include <cmath>

double rect_integral(double a, double b, int n, int f)
{
    using std::pow ;
    std::function<double(double)> 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.