gpt4 book ai didi

c - 最后通过一个循环,在函数调用时表现出奇怪和隐含的错误?

转载 作者:太空宇宙 更新时间:2023-11-04 05:43:06 25 4
gpt4 key购买 nike

我的代码:

#define RUNS 4

int main (int argc, char *argv[])
{
srand(time(NULL));

int i;
int _length;
float _totalTimeToReachLength;
float _arrayOfTimes[RUNS - 1];
float _sumDev = 0.0;

for (i = 0; i < RUNS; i++)
{
float localHopNumber = GenerateHops(_length); //pass length into hops method.

_arrayOfTimes[i] = localHopNumber; //add hop number generated to array.
_sumDev = (_sumDev + (_arrayOfTimes[i]-(_totalTimeToReachLength / RUNS)) * (_arrayOfTimes[i]-(_totalTimeToReachLength / RUNS))); //work out sd.
_totalTimeToReachLength = _totalTimeToReachLength + _arrayOfTimes[i];

printf("Current increment for average: %f\n", _totalTimeToReachLength);
printf("Item added to array: %f\n", _arrayOfTimes[i]);
}

printf("The standard deviation of times is: %f\n", sqrt(_sumDev/RUNS));
printf("The standard average of times is: %f\n", _totalTimeToReachLength / RUNS);

return 0;
}

为什么最后一个循环搞砸了?

最佳答案

数组声明需要:

float _arrayOfTimes[RUNS];

OP 中使用 RUNS-1 的声明将其声明为 3 个元素的数组(但您在其中存储了 4 个值)。

关于c - 最后通过一个循环,在函数调用时表现出奇怪和隐含的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13040095/

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