gpt4 book ai didi

无法理解数组的 c 输出

转载 作者:行者123 更新时间:2023-11-30 21:05:14 24 4
gpt4 key购买 nike

我无法理解这个循环的输出,N=64:

int t;
double new_y_avg[N];
/* ... */
for(t = 0; t<N; t++){
printf("new_y_avg[%d] = %f \t new_y_avg[%d] = %f \t new_y_avg[%d]= %f \n",
((t+1)%N), new_y_avg[(t+1)%N],
t, new_y_avg[t%N],
((t-1)%N), new_y_avg[(t-1)%N]
);
fflush(stdout);
}

给出以下输出:

    new_y_avg[1] = 0.164471      new_y_avg[0] = 0.429837     new_y_avg[-1]= -13.421363 
new_y_avg[2] = 0.062036 new_y_avg[1] = 0.164471 new_y_avg[0]= 0.429837
new_y_avg[3] = 0.023910 new_y_avg[2] = 0.062036 new_y_avg[1]= 0.164471
new_y_avg[4] = 0.009836 new_y_avg[3] = 0.023910 new_y_avg[2]= 0.062036
new_y_avg[5] = 0.004009 new_y_avg[4] = 0.009836 new_y_avg[3]= 0.023910
new_y_avg[6] = 0.001817 new_y_avg[5] = 0.004009 new_y_avg[4]= 0.009836
new_y_avg[7] = -nan new_y_avg[6] = 0.001817 new_y_avg[5]= 0.004009
new_y_avg[8] = -nan new_y_avg[7] = 0.000410 new_y_avg[6]= 0.001817
new_y_avg[9] = -nan new_y_avg[8] = -0.000260 new_y_avg[7]= 0.000410
new_y_avg[10] = -nan new_y_avg[9] = -0.000787 new_y_avg[8]= -0.000260
new_y_avg[11] = -nan new_y_avg[10] = -0.001049 new_y_avg[9]= -0.000787
new_y_avg[12] = -nan new_y_avg[11] = -0.001614 new_y_avg[10]= -0.001049
new_y_avg[13] = -nan new_y_avg[12] = -0.001979 new_y_avg[11]= -0.001614
new_y_avg[14] = -nan new_y_avg[13] = -0.001137 new_y_avg[12]= -0.001979
new_y_avg[15] = -nan new_y_avg[14] = -0.000353 new_y_avg[13]= -0.001137
new_y_avg[16] = 0.000516 new_y_avg[15] = -0.000379 new_y_avg[14]= -0.000353
new_y_avg[17] = -nan new_y_avg[16] = 0.000516 new_y_avg[15]= -0.000379
new_y_avg[18] = -nan new_y_avg[17] = 0.000855 new_y_avg[16]= 0.000516
new_y_avg[19] = -nan new_y_avg[18] = 0.000209 new_y_avg[17]= 0.000855
new_y_avg[20] = 0.000199 new_y_avg[19] = 0.000190 new_y_avg[18]= 0.000209
new_y_avg[21] = -nan new_y_avg[20] = 0.000199 new_y_avg[19]= 0.000190
new_y_avg[22] = -nan new_y_avg[21] = 0.000304 new_y_avg[20]= 0.000199
new_y_avg[23] = -nan new_y_avg[22] = -0.000368 new_y_avg[21]= 0.000304
new_y_avg[24] = -nan new_y_avg[23] = 0.000930 new_y_avg[22]= -0.000368
new_y_avg[25] = -nan new_y_avg[24] = -0.000782 new_y_avg[23]= 0.000930
new_y_avg[26] = -nan new_y_avg[25] = -0.002568 new_y_avg[24]= -0.000782
new_y_avg[27] = -nan new_y_avg[26] = -0.001959 new_y_avg[25]= -0.002568
new_y_avg[28] = -nan new_y_avg[27] = -0.001674 new_y_avg[26]= -0.001959
new_y_avg[29] = -nan new_y_avg[28] = -0.001250 new_y_avg[27]= -0.001674
new_y_avg[30] = -nan new_y_avg[29] = -0.000087 new_y_avg[28]= -0.001250
new_y_avg[31] = -0.000031 new_y_avg[30] = -0.000804 new_y_avg[29]= -0.000087
new_y_avg[32] = -nan new_y_avg[31] = -0.000031 new_y_avg[30]= -0.000804
new_y_avg[33] = -0.000031 new_y_avg[32] = 0.000537 new_y_avg[31]= -0.000031
new_y_avg[34] = -nan new_y_avg[33] = -0.000031 new_y_avg[32]= 0.000537
new_y_avg[35] = -0.000087 new_y_avg[34] = -0.000804 new_y_avg[33]= -0.000031
new_y_avg[36] = -nan new_y_avg[35] = -0.000087 new_y_avg[34]= -0.000804
new_y_avg[37] = -0.001674 new_y_avg[36] = -0.001250 new_y_avg[35]= -0.000087
new_y_avg[38] = -nan new_y_avg[37] = -0.001674 new_y_avg[36]= -0.001250
new_y_avg[39] = -nan new_y_avg[38] = -0.001959 new_y_avg[37]= -0.001674
new_y_avg[40] = -nan new_y_avg[39] = -0.002568 new_y_avg[38]= -0.001959
new_y_avg[41] = -nan new_y_avg[40] = -0.000782 new_y_avg[39]= -0.002568
new_y_avg[42] = -nan new_y_avg[41] = 0.000930 new_y_avg[40]= -0.000782
new_y_avg[43] = -nan new_y_avg[42] = -0.000368 new_y_avg[41]= 0.000930
new_y_avg[44] = -nan new_y_avg[43] = 0.000304 new_y_avg[42]= -0.000368
new_y_avg[45] = -nan new_y_avg[44] = 0.000199 new_y_avg[43]= 0.000304
new_y_avg[46] = -nan new_y_avg[45] = 0.000190 new_y_avg[44]= 0.000199
new_y_avg[47] = -nan new_y_avg[46] = 0.000209 new_y_avg[45]= 0.000190
new_y_avg[48] = 0.000516 new_y_avg[47] = 0.000855 new_y_avg[46]= 0.000209
new_y_avg[49] = -nan new_y_avg[48] = 0.000516 new_y_avg[47]= 0.000855
new_y_avg[50] = -nan new_y_avg[49] = -0.000379 new_y_avg[48]= 0.000516
new_y_avg[51] = -nan new_y_avg[50] = -0.000353 new_y_avg[49]= -0.000379
new_y_avg[52] = -0.001979 new_y_avg[51] = -0.001137 new_y_avg[50]= -0.000353
new_y_avg[53] = -nan new_y_avg[52] = -0.001979 new_y_avg[51]= -0.001137
new_y_avg[54] = -nan new_y_avg[53] = -0.001614 new_y_avg[52]= -0.001979
new_y_avg[55] = -nan new_y_avg[54] = -0.001049 new_y_avg[53]= -0.001614
new_y_avg[56] = -nan new_y_avg[55] = -0.000787 new_y_avg[54]= -0.001049
new_y_avg[57] = -nan new_y_avg[56] = -0.000260 new_y_avg[55]= -0.000787
new_y_avg[58] = -nan new_y_avg[57] = 0.000410 new_y_avg[56]= -0.000260
new_y_avg[59] = -nan new_y_avg[58] = 0.001817 new_y_avg[57]= 0.000410
new_y_avg[60] = -nan new_y_avg[59] = 0.004009 new_y_avg[58]= 0.001817
new_y_avg[61] = -nan new_y_avg[60] = 0.009836 new_y_avg[59]= 0.004009
new_y_avg[62] = -nan new_y_avg[61] = 0.023910 new_y_avg[60]= 0.009836
new_y_avg[63] = -nan new_y_avg[62] = 0.062036 new_y_avg[61]= 0.023910
new_y_avg[64] = -nan new_y_avg[63] = 0.164471 new_y_avg[62]= 0.062036

而以下代码:

binned_file = fopen("binned_avg.txt","w+");
for(t=0;t<N;t++){
fprintf(binned_file,"%d \t %f \n",
t,new_y_avg[t]);
}
fclose(binned_file);

提供合理的输出:

0    0.429837 
1 0.164471
2 0.062036
3 0.023910
4 0.009836
5 0.004009
6 0.001817
7 0.000410
8 -0.000260
9 -0.000787
10 -0.001049
11 -0.001614
12 -0.001979
13 -0.001137
14 -0.000353
15 -0.000379
16 0.000516
17 0.000855
18 0.000209
19 0.000190
20 0.000199
21 0.000304
22 -0.000368
23 0.000930
24 -0.000782
25 -0.002568
26 -0.001959
27 -0.001674
28 -0.001250
29 -0.000087
30 -0.000804
31 -0.000031
32 0.000537
33 -0.000031
34 -0.000804
35 -0.000087
36 -0.001250
37 -0.001674
38 -0.001959
39 -0.002568
40 -0.000782
41 0.000930
42 -0.000368
43 0.000304
44 0.000199
45 0.000190
46 0.000209
47 0.000855
48 0.000516
49 -0.000379
50 -0.000353
51 -0.001137
52 -0.001979
53 -0.001614
54 -0.001049
55 -0.000787
56 -0.000260
57 0.000410
58 0.001817
59 0.004009
60 0.009836
61 0.023910
62 0.062036
63 0.164471

由于打印到文件有效(并且输出与我的代码的其余部分合理),问题不应该是 new_y_avg。因此,我不明白为什么在第一个循环中,一行给出 new_y_avg[60] = -nan ,另一行给出 new_y_avg[60]= 0.009836 。我尝试用 valgrind 运行它,但没有给出错误。

编辑:我正在使用同一个数组,我发布的代码是一个接一个(我尝试在循环内调用函数时在终端上打印,它给出了荒谬的值(不,该函数不会更改数组)),我更正了从 1 而不是 0 开始的循环,但仍然存在相同的问题,循环需要在 N 而不是 N-1 处关闭,因为我正在调用其他另一个函数。我无法发布整个代码,因为:它调用的时间太长,它分为多个文件,最重要的是它是大学的作业。稍后我将尝试按照 Yunnosch 的说明更改循环并发布结果

最佳答案

您将 new_y_avg 定义为 double new_y_avg[N] 中的 double float 数组。

但是,您在 printf 中使用了不正确的格式说明符。

您使用了 printf("new_y_avg[%d] = %f ...,而 double 的格式说明符是 %lf。

介绍了undefined behaviour (UB)这意味着编译器可以做任何事情。有时它可能会起作用,但它是不可预测的。

关于无法理解数组的 c 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55663574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com