最后一次通过一个循环表现出函数调用上的奇怪和隐式错误?

dev*_*546 0 c for-loop

我的代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

为什么它在最后一个循环中搞乱了?

Mar*_*ins 5

数组声明需要是:

float _arrayOfTimes[RUNS];
Run Code Online (Sandbox Code Playgroud)

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