You would like to find the area under the curve yfx between
Solution
#include <stdio.h>
#include <math.h>
//Note 4th parameter in function pointer means taking address of another function as pointer
float trap(float a, float b, int n, float (*f)(float val)) {
float h = (b-a)/n;
float delta = f(a)+f(b);
for(int i = 1; i < n; i++){
delta+= 2*f(a+i*h);
}
float area=h/2*delta;
return area;
}
float g(float x) {
return x*x*sin(x);
}
float h(float x) {
return sqrt(4-x*x);
}
int main() {
float a=0;
float b=3.14159;
int n=0;
for(n=0; n<8; n++){
printf(\"value for trap for g(x) for n=%d is %f\ \",n,trap(a,b,2^n,&g));
}
a=-2;
b=2;
n=0;
for(n=0; n<8; n++){
printf(\"value for trap for h(x) for n=%d is %f\ \",n,trap(a,b,2^n,&h));
}
printf(\"Area of a circle with radius 2 is float %f\ \",3.1459*2*2/2);
printf(\"End of Main...\ \");
}
