Here是我的完整代码,以防你想引用它。
所以我有一个3D阵列可以存储特定天/月的高温和低温。我需要通过这个函数传递数组:
double average_array(int A[], int SIZE)
{
int total=0;
double avg;
for (int i = 0; i < SIZE; i++)
{
total = total + A[i];
}
avg = (double) total / (double) SIZE;
return avg;
}
我尝试过像
这样的事情for (i=0; i<DAY; i++)
{
for (k=0; k<TEMP; k++)
{
cout << "The average for June was " << average_array(array[i][0], 3) << endl;
}
} break;
仅获得6月份的平均值,甚至
for (i=0; i<DAY; i++)
{
for (j=0; j<MONTH; j++)
{
for (k=0; k<TEMP; k++)
{
cout << "The average was " << average_array(array[i][j][k],3) << endl;
}
}
} break;
尝试打印整体平均值。没有打印(虽然编译得很好),而后者返回“错误:从'int'无效转换为'int *'[-fpermissive]”
如何通过函数传递数组的相应索引?
答案 0 :(得分:0)
如果数组array
为double array[MONTH][DAY][TEMP];
for (i=0; i<MONTH; i++)
{
for (j=0; j<DAY; j++)
{
cout << "The average was " << average_array(&array[i][j][0],TEMP) << endl;
// or average_array(array[i][j],TEMP)
}
}