函数不读取 2D 数组的 [i][0] 行

Function not reading a 2D array's [i][0] row

本文关键字:读取 2D 数组 函数      更新时间:2023-10-16

考虑以下数组:

arr[0][0] = 5.42; // val
arr[0][1] = (int)1;    // freq
arr[1][0] = 41.45;
arr[1][1] = (int)1; // freq
arr[2][0] = 10.55;
arr[2][1] = (int)1;// freq
arr[3][0] = 78.55;
arr[3][1] = (int)1;// freq

它应该表示(非累积的(频率表。因此每个值的频率为1。

现在考虑以下功能:

      const int SIZE = 1;
    double standardDeviation(double list[][SIZE], double mean, int size) {
    double sumSquared = NULL;
    double sum = NULL;
    double sumFx = NULL;
    int sumFreq = NULL;
    for(int i = 0; i < size; i++)
        sumFreq += list[i][1]; 
    return sumFreq;
    }

(显然缺少一些代码(但在目前的代码中,我的目标是返回频率的总和,应该是4,但它只返回索引为[i][0]的数组成员的值,基本上是值。

有什么想法吗?

您的数组没有被正确访问,因为您有:

const int SIZE = 1;

你需要的是:

const int SIZE = 2;

顺便说一句,这些初始化都没有意义:

double sumSquared = NULL;
double sum = NULL;
double sumFx = NULL;
int sumFreq = NULL;

NULL0,是的,但从语义上讲,它是用来指示空指针的,而且这些变量都不是指针。

参数被声明为double list[][1]

所以list[i][1]是越界的。