C++ 函数不读取2D数组';s[i][0]行
考虑以下数组:C++ 函数不读取2D数组';s[i][0]行,c++,arrays,C++,Arrays,考虑以下数组: 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 现在考虑以下函数: con
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;
}
const int SIZE=1;
双标准差(双列表[][大小],双平均值,整数大小){
双平方和=零;
双和=零;
双sumFx=NULL;
int-sumFreq=NULL;
对于(int i=0;i
(显然缺少一些代码)
但在目前的代码中,我的目标是返回频率之和,应该是4,但它只返回索引为[i][0]的数组成员的值,基本上是这些值
知道为什么吗?该参数被声明为
双列表[][1]
因此
list[i][1]
是不允许的。该参数被声明为double list[][1]
因此,
list[i][1]
是不允许的。您的数组没有被正确访问,因为您有:
const int SIZE = 1;
你需要做到:
const int SIZE = 2;
顺便说一句,这些初始化都没有意义:
double sumSquared = NULL;
double sum = NULL;
double sumFx = NULL;
int sumFreq = NULL;
NULL
是0
,是的,但从语义上讲,它用于指示空指针,并且这些变量都不是指针。您的数组没有被正确访问,因为您有:
const int SIZE = 1;
你需要做到:
const int SIZE = 2;
顺便说一句,这些初始化都没有意义:
double sumSquared = NULL;
double sum = NULL;
double sumFx = NULL;
int sumFreq = NULL;
NULL
是0
,是的,但在语义上它是用来表示空指针的,这些变量都不是指针。(int)
类型转换是怎么回事1已经是一个int
,当它进入数组时,它仍然会被转换成一个double
。这只是一个视觉提醒,提醒我这是频率线,Lol(int)
类型转换出了什么问题1已经是一个int
,当它进入数组时,它仍然会被转换成double