include stdio include stdlib include math double integ double double i

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double integ(double(*f)(double),int,int,int);
double f1(double);
double f2(double);
int main(void)
{
double s,a,b,(*p)(float);
int n,k,i;
do{
printf("Input number of F\n");
scanf("%d",&k);
}
while(k>2 || k<1);
for(i=1;i<=k;i++)
{
switch(i)
{
case 1:a=3;b=-1;p=f1;break;
case 2:a=5;b=1;p=f2;break;
}
n=35;
do
{
s=integ(p,a,b,n);
n*=2;
printf("integral=%lf n=%d\n",s,n);
}
while(fabs(s-integ(p,a,b,n)>1e-5));
printf("\n");
}
return 0;
}
double integ(double(*f)(double),int a,int b,int n)
{
double h,s=(f(a)+f(b))/2;
double i;
h=(double)(b-a)/n;
for(i=a+h;i<=b-h;i+=h)
s+=f(i);
return s*=h;
}
double f1(double x){
return 1/(3*x*x-2*x+4);
}
double f2(double x)
{
return cos(x)/(1+sin(x)*sin(x));
}