slauko
slauko

Reputation: 81

error: too few arguments to function ‘ptr’ , need to eval_time from a function

I have a small problem with my program. I want to eval the time from a function and the compiler gives me an error. I know were the problem is, but I just dont know how to fix it :/

#include <stdio.h>
#include <time.h>

int isPerfect(int zahl){
int zaehler1, zaehler2, summe;
for(zaehler1=1;zaehler1<=zahl;zaehler1++){
    summe=0;
    for(zaehler2=1;zaehler2<=zaehler1/2;zaehler2++){
        if(zaehler1%zaehler2==0){
        summe=summe+zaehler2;
        }
    }
}
return summe;
}

double eval_time(int(*ptr)(int)){
    time_t begin,end;
    begin=time(NULL);
    (*ptr)();  //compiler shows error here!
    end=time(NULL);
    return difftime(end,begin);
    }

int main(void){
    int zahl;
    for(zahl=1;zahl<=500;zahl++){
        if(isPerfect(zahl)==zahl){
        printf("%d ist eine perfekte Zahl!\n", zahl);
        }
    }
    printf("Die Zeit die gebraucht wurde: %.2lf s\n",eval_time(isPerfect));
return 0;
}

So my question is what do i need to change there so it evals the time from the funtion "isPerfect" ? Sorry my variables are in german, I hope that's not a problem ;)

Upvotes: 2

Views: 642

Answers (1)

Bo Persson
Bo Persson

Reputation: 92211

You have to pass a parameter, perhaps like this:

double eval_time(int(*ptr)(int), int zahl){
    time_t begin,end;
    begin=time(NULL);
    (*ptr)(zahl);
    -------^
    end=time(NULL);
    return difftime(end,begin);
}

Another problem is that you might have to call the functions lots and lots of times between begin and end to actually get a measurable difference in the time() values.

Upvotes: 2

Related Questions