2

For my homework in programming, I have to write a program that calculates the integral of a function

main.c file:

    #include <stdio.h>
    #include <math.h>
    #include "integrand.h"
    #define M 64
    #define N 64
    #define dif 0.01
    #define max 8 

    float (*pf)(float,float);

    void integrate(float pf(float x,float y)){
    int i,j,k=0,l=1; 
    float a,b,c,d,dx,dy,dA,x[i],y[j],r=0,r_old;

    printf("Enter the integration bounds a,b,c,d separated by commas: \n\n");scanf("%f,%f,%f,%f",&a,&b,&c,&d); 

    do{
    r_old = r;

    dx = (b - a)/(l*M);
    dy = (d - c)/(l*N);
    dA = dx * dy;

    for(i=0;i<(l*M)-1;i++){
        x[i] = a + (i + 0.5)*dx;
    }

    for(j=0;j<(l*N)-1;j++){
        y[j] = c + (j + 0.5)*dy;
    }

    for(i=0;i<(l*M)-1;i++){
        for(j=0;j<(l*N)-1;j++){
            r+=(((*pf)(x[i],y[j]))*dA);
            }}

    k++;
    l*=2;
    }while(k<max || abs(r-r_old)<=dif*abs(r));

    printf("Integral = %f",r);

    return;
    }

    int main(void)
    {
    float x,y;
    int q;

    printf("Choose an integrand:\n\n (1) f_1(x,y) = x^3 + 3y^2*x + 7x^2*y + 10 \n\n (2) f_2(x,y) = cos(0.1*x^5 + y^2) - sin(y*x)^5 + log(|x-y|+1) + 2 \n\n (3) f_3(x,y) = sin(x^2+y^2) + exp(-50*(x^2+y^2))\n\n (4) f_4(x,y) = f_3(f_1(x,y),f_2(x,y))\n\n (5) QUIT \n\n");

    do{
    scanf("%d",&q);
    switch(q){
        case 1:
            pf = f_1;
            integrate((&pf)(x,y));
            break;
                case 2:
            pf = f_2;
            integrate((&pf)(x,y));
            break;
        case 3:
            pf = f_3;
            integrate((&pf)(x,y));
            break;
        case 4:
            pf = &f_4;
            integrate((&pf)(x,y));
            break;

    }}while(q!=5);

    return 0;
    }

integrand.c file:

    #include <math.h>

    float f_1(float x, float y){return (pow(x,3) + 3*pow(y,2)*x + 7*pow(x,2)*y + 10);}

    float f_2(float x, float y){return (cos(0.1*pow(x,5)+pow(y,2)) - pow(sin(y*x),5) + log(fabs(x-y)+1) + 2);}

    float f_3(float x, float y){return (sin(pow(x,2)+pow(y,2)) + pow(M_E,-50*(pow(x,2)+pow(y,2))));}

    float f_4(float x, float y){return (f_3(f_1(x,y),f_2(x,y)));}

integrand.h file:

    #ifndef INTEGRAND_H
    #define INTEGRAND_H

    extern float f_1(float x, float y);
    extern float f_2(float x, float y);
    extern float f_3(float x, float y);
    extern float f_4(float x, float y);

    #endif // INTEGRAND_H

The error in the title shows up in this part in all four cases:

    integrate((&pf)(x,y));

I can't figure out where exactly the issue lies, I've tried swapping the & for * or deleting it entirely, but that only leads to an error saying that the argument type is incompatible for argument 1 of 'integrate'

6
  • 4
    What about just doing integrate(pf);? Commented Jan 4, 2018 at 11:15
  • ... or directly integrate(f_1); Commented Jan 4, 2018 at 11:19
  • The task says I have to do this with a function pointer, so that won't work directly. integrate(pf); helped fix the issue though, thanks! But my new problem is that the program crashes... Commented Jan 4, 2018 at 11:22
  • 1
    @pavus but the proposed solution do use a function pointer. Commented Jan 4, 2018 at 11:23
  • @MichaelWalz what I'm saying is that I can't use integrate(f_1); because of my task saying that I have to do it with a function pointer Commented Jan 4, 2018 at 11:29

1 Answer 1

1

pf is a function pointer, you need to pass it to integrate without any additional parameters. Since you call the function the same way in all four branches, move the call to integrate outside switch:

do{
    scanf("%d", &q);
    switch(q){
        case 1: pf = f_1; break;
        case 2: pf = f_2; break;
        case 3: pf = f_3; break;
        case 4: pf = f_4; break;
        default: pf = NULL; break;
    }
    if (pf) {
        integrate(pf);
    }
} while (q != 5);

This pattern of switch use could also be replaced with an array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.