Skip to content

Instantly share code, notes, and snippets.

@kaneshin
Created June 3, 2012 12:13
Show Gist options
  • Save kaneshin/2863234 to your computer and use it in GitHub Desktop.
Save kaneshin/2863234 to your computer and use it in GitHub Desktop.
#include <stdio.h>
double
integrate(
double a,
double b,
double (*f)(double)
){
int i, size = 1000;
double area = 0., d = (b - a) / size;
for (i = 0; i < size; i++) {
area += f(a + i * d) * d;
}
return area;
}
double
f1(double x) {
return x;
}
double
f2(double x) {
return x * x;
}
int
main(int argc, char* argv[]) {
printf("%f\n", integrate(0., 3., f1));
printf("%f\n", integrate(0., 3., f2));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment