gpt4 book ai didi

c - 变量不保持其值

转载 作者:行者123 更新时间:2023-11-30 16:39:04 25 4
gpt4 key购买 nike

在下面的代码片段中,由于某种原因,错误的值在 FOR 循环之外不成立。

    eror = 0 ;
for (i=0; i<n; i++)
{
predict = intercept + (slope * data[i][0]) ;
printf("In the FOR loop eror = %lf\n", eror) ;
eror +=(data[i][1] - predict) ;
}
printf("Out of FOR loop eror = %lf\n", eror) ;

一些输出是...

    In the FOR loop eror =  -0.028400
In the FOR loop eror = -0.011216
In the FOR loop eror = 0.012196
In the FOR loop eror = 0.019527
In the FOR loop eror = 0.024293
In the FOR loop eror = 0.021309
In the FOR loop eror = 0.013337
Out of FOR loop eror = -0.000000

任何人都可以解释为什么 FOR 循环内有一个值,但一旦变量错误退出 FOR 循环就没有值?

最佳答案

尝试:

eror = 0 ;
for (i=0; i<n; i++)
{
predict = intercept + (slope * data[i][0]) ;
eror +=(data[i][1] - predict) ;
printf("In the FOR loop eror = %lf\n", eror) ;
}
printf("Out of FOR loop eror = %lf\n", eror) ;

关于c - 变量不保持其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47213784/

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