函数不读取 2D 数组的 [i][0] 行
Function not reading a 2D array's [i][0] row
考虑以下数组:
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;
NULL
是0
,是的,但从语义上讲,它是用来指示空指针的,而且这些变量都不是指针。
参数被声明为double list[][1]
。
所以list[i][1]
是越界的。
相关文章:
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 读取字符串文本输入以创建 2D 矢量
- 如何从文件中读取值,然后使用它们来初始化 2D 数组C++
- 如何将逗号分隔的文件读取为 2D int 数组?
- 从文本文件中读取一行并将其存储在 C++ 中的 2D 矢量中
- 当我在将文件读取到C字符串的2D数组后尝试对任何内容进行定制时,C++分段错误
- 将文本文件读取到2D数组中,但数组开始为超过数组[39][]的任何内容输出胡言乱语
- C++2D网格数组,从文件中读取和插入数组值
- C++将 txt 数据和字符读取到 2D 数组中
- 从文本文件读取到 2D 数组中
- 未正确读取 2D 数组元素
- 从文本文件读取 2D 数组错误
- 尝试读取 2D 数组时"no matching function for call"
- 从二进制读取 2D 浮点数组,从 char* 转换为浮点数[2]
- 读取 2D 矢量
- 从控制台读取 2D 数组
- 从制表符分隔的文件中读取2D数据并存储在矢量C++中
- 在c++中使用boost::spirit读取2D数组
- 函数不读取 2D 数组的 [i][0] 行
- 读取2d数组并显示它(c++)