这道题是 C Primer Plus第十章的第十三题,我写了a,b,c三小题,没报错但运行不对,请问我这样写错在哪里?
#define ROWS 3
#define COLS 5
// 用户输入数据
void save(double a[][COLS], int row)
{
for (int i = 0; i < COLS; i++)
{
scanf("%lf", &a[row][i]);
}
}
// 输出数组元素
void print(double a[][COLS], int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < COLS; j++)
{
printf("%15lf", a[i][j]);
}
printf("\n");
}
}
// 计算数组单独一行的平均值
double ave(double a[COLS])
{
double sum = 0.0;
for (int i = 0; i < COLS; i++)
{
sum += a[i];
}
return sum / COLS;
}
// 计算数组的平均值
double ave2(double a[][COLS], int rows)
{
double sum = 0.0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < COLS; j++)
{
sum += a[i][j];
}
return sum / (rows * COLS);
}
// 返回数组最大的元素值
double _max(double a[][COLS], int rows)
{
double max = a[0][0];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < COLS; j++)
{
if (a[i][j] > max)
max = a[i][j];
}
}
return max;
}
int main()
{
double a[ROWS][COLS];
for (int row = 0; row < ROWS; row++)
{
save(a, row);
}
print(a, ROWS);
for (int row = 0; row < ROWS; row++)
{
printf("%lf ", ave(a[row]));
}
printf("\n");
printf("%lf\n", ave2(a, ROWS));
printf("%lf\n", _max(a, ROWS));
return 0;
}