我想查找二维数组中所有列的平均值并将它们存储到一维数组中。 从那个一维数组中,我想返回最低数字的位置。 这里是我的代码,它确实产生了一个答案(没有编译/运行时错误),但它给出的答案是不正确的。查找二维数组中每列的平均值
int function(){
int m, n,p;
for(m = 0; m < NROW; m++){//I think the problem must be within this set of for loops.
for(n = 0; n < NCOL; n++){
averages[n] += twoD[n][m]; //averages is an array of type double
}
}
for(p = 0; p < NCOL; p++){
averages[p] = (((double)averages[p])/((double)NROW));
}
int d, location;
int lowest = averages[0];
for (d = 1 ; d < NCOL ; ++d)
{
if (averages[d] < lowest)
{
lowest = averages[d];
location = d;
}
}
return location;
}
你的'averages'是一个整数列吗? – dragosht 2014-10-07 07:25:10
平均数是双打数组。 – user2159301 2014-10-07 07:26:02
您是否初始化平均值数组? – Igor 2014-10-07 07:26:30