C language to achieve a definite integral method
- 2020-04-02 02:57:42
- OfStack
In this paper, an example is given to illustrate the method of definite integral in C language. Share with you for your reference. The specific implementation method is as follows:
#include <cmath>
#include <cstdio>
#define ACC 1000
float solve(float (*p)(float),float up,float down,int acc);
float fun_exp(float x);
float fun_qua(float x);
void main(){
char selection;
float up,down;
while(printf(" Please select the integrand :n"),printf("1 , exp(x) 2 , x+1 n"),scanf("%c",&selection),selection != '#'){
printf(" Please enter the limits of integration : ");
scanf("%f,%f",&up,&down);
switch(selection){
case '1':
printf(" As a result, : %4.4fn",solve(fun_exp,up,down,ACC));
break;
case '2':
printf(" As a result, : %4.4fn",solve(fun_qua,up,down,ACC));
break;
}
}
}
float solve(float (*p)(float),float up,float down,int acc){
float sum,base,area;
area = 0;
sum = 0;
base = (up-down)/acc;
for(int i=0; i
area = base*((*p)(down+i*base));
sum+=area;
}
return sum;
}
float fun_exp(float x){
return exp(x);
}
float fun_qua(float x){ //Quadratics
return x+1;
}
Hope that the article described in the C programming language for you to help.