一个求差商的C语言程序,我检查了很久都没找到问题出在哪里.希望能得到高手的帮助。

2024-12-19 11:26:27
推荐回答(5个)
回答1:

老大你的for循环后边全都有分号,那个就更本部循环好吧!!!!
修改以后的代码:

int main()
{int i;
float A[4],B[3],C[2],D,
x[5]={0.4,0.55,0.65,0.8,0.9},y[5]={0.41075,0.57815,0.69675,0.88811,1.02652};
for(i=0;i<5;i++)
A[i]=(y[i+1]-y[i])/(x[i+1]-x[i]);
for(i=0;i<4;i++)
printf("A[%d]=%f\t",i,A[i]);
for(i=0;i<4;i++)
B[i]=(A[i+1]-A[i])/(x[i+2]-x[i]);
for(i=0;i<3;i++)
printf("B[%d]=%f\t",i,B[i]);
for(i=0;i<3;i++)
C[i]=(B[i+1]-B[i])/(x[i+3]-x[i]);
for(i=0;i<2;i++)
printf("C[%d]=%f\t",i,C[i]);
D=(C[i+1]-C[i])/(x[4]-x[0]);
printf("D=%f\t",D);
return 0;
}

修改以后的结论:
A[0]=1.116000 A[1]=1.186001 A[2]=1.275733 A[3]=1.384101 B[0]=0.280003 B[1]=0.358930 B[2]=0.433471 C[0]=0.197316 C[1]=0.212976 D=-0.397131

回答2:

for语句后面加分号的话,每次都执行空语句,下面的计算赋值只会执行一次。

#include
#include

int main() {
int i;
float A[4], B[3], C[2], D, x[5] = {
0.4, 0.55, 0.65, 0.8, 0.9
}, y[5] = {
0.41075, 0.57815, 0.69675, 0.88811, 1.02652
};
for (i = 0; i < 4; i++)
A[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
for (i = 0; i < 4; i++)
printf("A[%d]=%f\t", i, A[i]);
printf("\n");
for (i = 0; i < 3; i++)
B[i] = (A[i + 1] - A[i]) / (x[i + 2] - x[i]);
for (i = 0; i < 3; i++)
printf("B[%d]=%f\t", i, B[i]);
printf("\n");
for (i = 0; i < 2; i++)
C[i] = (B[i + 1] - B[i]) / (x[i + 3] - x[i]);
for (i = 0; i < 2; i++)
printf("C[%d]=%f\t", i, C[i]);
printf("\n");
D = (C[i + 1] - C[i]) / (x[4] - x[0]);
printf("D=%f\t", D);
printf("\n");
system("pause");
return 0;
}

回答3:

for(i=0;i<5;i++);你自己写的最大值为4,当然有A[4]了

回答4:

#include

void main()

{

int i;

float A[4],B[3],C[2],D,

x[5]={0.4,0.55,0.65,0.8,0.9},y[5]={0.41075,0.57815,0.69675,0.88811,1.02652};

for(i=0;i<4;i++)//i<4而不是i<5;去掉for后面的分号,不去掉的话就只会重复4条空语句,而下一句你本想它执行4吃的语句就只会执行一次了咯

 A[i]=(y[i+1]-y[i])/(x[i+1]-x[i]);

for(i=0;i<4;i++)

  printf("A[%d]=%f\t",i,A[i]);

for(i=0;i<4;i++)

  B[i]=(A[i+1]-A[i])/(x[i+2]-x[i]);

for(i=0;i<3;i++)

  printf("B[%d]=%f\t",i,B[i]);

for(i=0;i<3;i++)

  C[i]=(B[i+1]-B[i])/(x[i+3]-x[i]);

for(i=0;i<2;i++)

  printf("C[%d]=%f\t",i,C[i]);

D=(C[i+1]-C[i])/(x[4]-x[0]);

printf("D=%f\t",D);

}

回答5:

回答:
float x[5];
for (i = 0; i < 5; i++)
scanf("%f",&x[i]);
for (i = 0; i < 5; i++)
printf("%f\n",x[i]);
完全可以,你看看你的。。。