用C语言计算K-L信息量法,运行之后答案出现错误,请大家帮忙看看
程序代码:
#define N 72 #define L 12 #include <stdio.h> #include <math.h> void main() { int i,j=0,t,l; float x[N]; float y[N]; float pt[N],qt[N]; float sum_x=0; float sum_y=0; float min; int l_num; int m; long double kl[2*L+1]={0}; printf("please center the element of the CPI\n"); for(i=0;i<N;i++) scanf("%f",&x[i]); printf("please center the element of the Candidate Index\n"); for(i=0;i<N;i++) scanf("%f",&y[i]); for(i=0;i<N;i++) { sum_x+=x[i]; sum_y+=y[i]; } printf("sum_x=%f\n",sum_x); printf("sum_y=%f\n",sum_y); for(i=0;i<N;i++) { pt[i]=y[i]/sum_y; qt[i]=x[i]/sum_x; } for(m=0;m<N;m++) { printf("pt[%d]=%f\n",m,pt[m]); printf("qt[%d]=%f\n",m,qt[m]); for(l=-L;l<0;l++) { for(t=-l+1;t<=N;t++) { kl[j]+=pt[t-1]*log(fabs(pt[t-1]/qt[t+l-1])); } printf("kl[%d]=%f\n",j,kl[j]); j=j+1; } printf("j11=%d\n",j); for(l=0;l<=L;l++) { for(t=1;t<=N-l;t++) { kl[j]+=pt[t-1]*log(fabs(pt[t-1]/qt[t+l-1])); } printf("kl[%d]=%f\n",j,kl[j]); j=j+1; } printf("j22=%d\n",j); for(i=0;i<j;i++) { printf("kl[%d]= ",i-L); printf("%f\n",kl[i]); } min=fabs(kl[0]); l_num=0; for(i=1;i<j;i++) if(min>fabs(kl[i])) {min=fabs(kl[i]);l_num=i;} l_num=l_num-L; printf("l_number is =%d\n",l_num); printf("the min is=%.16f\n",min); } }
以上是错误的截图,请帮忙修改一下,谢谢
这个是输入的数据:
CPI=87.88
88.35
89.13
90.31
91.67
93.06
94.46
95.62
96.48
97.27
98.30
99.53
100.41
100.72
100.67
100.69
100.83
101.02
101.08
101.17
100.98
100.57
100.01
99.53
99.40
99.60
100.08
100.90
102.32
104.20
106.24
107.85
108.46
107.93
106.37
104.64
103.40
103.10
103.47
103.99
104.26
103.87
102.66
100.84
99.16
98.22
98.57
100.16
102.61
105.32
107.37
108.18
107.55
106.08
105.02
105.10
106.47
108.49
110.33
111.01
110.17
107.95
105.24
103.24
102.80
104.04
106.76
110.40
114.33
118.20
121.85
124.76
Index=
103.7
102.6
103.4
102.6
103
103
102.6
103.4
103.9
102.8
103.9
104.1
101.8
102.3
101.2
101.2
101.1
101.7
101.8
102.4
103.2
103.4
103.2
103.2
102.8
102.4
102.9
102.5
102.8
102.5
102.5
101.7
101.1
101.2
101.5
101.4
101
101.5
101.5
101.3
101.2
101.5
101.5
101.9
101.8
101.7
101.4
101.9
101.7
102.7
102.2
102.3
101.8
101.6
101.3
100.9
101.2
100.9
101.4
101.2
102
100.4
100.7
100.7
101.4
101.5
101.7
102
102
102.4
102.2
102.4