Created
June 3, 2012 12:13
-
-
Save kaneshin/2863234 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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