我正在尝试从用户输入中存储3组5个双号。 我需要将信息存储在3 x 5阵列中,并计算每组五个值的平均值。
我无法弄清楚如何解决两个错误。
第一个错误: hw9.c:27:2错误:'set_average'的参数1的不兼容类型 set_average(array [ROW] [COL]); ^
第二个错误: hw9.c:8:6:注意:预期'double(*)[5]'但参数类型为'double' void set_average(double array [ROW] [COL]);
感谢您提供任何帮助和建议。
#include <stdio.h>
#define ROW 3
#define COL 5
void set_average(double array[ROW][COL]);
void all_average(double array[ROW][COL]);
void find_largest(double array[ROW][COL]);
int main(void)
{
double array[ROW][COL];
int i, j;
printf("Enter three sets of five double numbers.\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 5; j++)
{
printf("Enter elements until done.\n");
printf("Enter %d%d: ",i+1,j+1);
scanf("%le", &array[i][j]);
}
printf("Done entering numbers.\n");
printf("Now it's time to compute the average of each set of five values\n");
set_average (array[ROW][COL]);
return 0;
}
void set_average(double array[ROW][COL])
{
int r; //row
int c;
double sum;
double avg; //average
for (r = 0; r < ROW; r++)
for (c = 0; c < COL; c++)
{
sum += array[r][c];
}
avg = sum / 5;
printf("The average is %le\n", avg);
}
答案 0 :(得分:0)
您正在调用函数
set_average (array[ROW][COL]);
而不是
set_average (array);
函数定义为void set_average(double array[ROW][COL])
所以它需要一个ROW
数组。每个都有一个COL
项的数组,是双打的。报告错误是因为仅使用一个double
作为参数调用函数。
答案 1 :(得分:0)
您需要将完整数组传递给函数
set_average (array);
正在传递
set_average (array[ROW][COL]);
它只发送单个值,即array[3][5]
在set_average
功能中,您尚未初始化double sum;
的值。用零初始化sum
,否则输出错误。
答案 2 :(得分:0)
try this:
------------------
#include <stdio.h>
#define ROW 3
#define COL 5
void set_average(double array[][COL]);
int main(void)
{
double array[ROW][COL];
int i, j;
printf("Enter three sets of five double numbers.\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 5; j++)
{
printf("Enter elements until done.\n");
printf("Enter %d%d: ",i+1,j+1);
scanf("%le", &array[i][j]);
}
printf("Done entering numbers.\n");
printf("Now it's time to compute the average of each set of five values\n");
set_average( array );
return 0;
}
void set_average( double array[ ][5] )
{
int r; //row
int c;
double sum;
double avg; //average
for (r = 0; r < ROW; r++)
for (c = 0; c < COL; c++)
{
sum += array[r][c];
}
avg = sum / 5;
printf("The average is %le\n", avg);
}