Skip to content

Instantly share code, notes, and snippets.

@mperlet
Created October 20, 2015 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mperlet/2fa00cf3c67e31929c56 to your computer and use it in GitHub Desktop.
Save mperlet/2fa00cf3c67e31929c56 to your computer and use it in GitHub Desktop.
Beispiel für functionpointer in c
#include <stdio.h>
#include <math.h>
double f1(double x) {return x*x*x;}
double xq(double x) {return x*x;}
double sin_func(double x) {return sin(x);}
double f(double begin, double end, double pieces, double (*fp)(double))
{
double delta_x = (end-begin)/pieces;
//printf("delta_x = %f\n", delta_x);
int i;
double return_value = 0;
for(i=0;i<=pieces;i++) {
return_value += fp(begin+(i*delta_x)+(delta_x*0.5))*delta_x;
}
return return_value;
}
int main( int argc, char **argv)
{
//int i = 0;
//double result = f(0,2,100);
//double (fp*)(double);
printf("Result [0,2]: %f\n", f(0,2,100, xq));
printf("Result [1,2]: %f\n", f(1,2,100, xq));
printf("Result [-1,1]: %f\n", f(-1,1,100, xq));
printf("Result [4,7]: %f\n", f(4,7,100, xq));
printf("Result [0,2] 20rec: %f\n", f(0,2,20, xq));
printf("Result [0,2] 60rec: %f\n", f(0,2,60, xq));
printf("Result [0,2] 200rec x^3: %f\n", f(0,2,200, f1));
printf("Result [0,PI] 200rec: %f\n", f(0,M_PI,20, sin_func));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment