将二维数组求每组平均数,所有数据平均数和找出最大值
做到一道题目要求3*5二维数组,求每组平均数,所有数据平均数,找出最大值。我用三个子函数按顺序分别处理,但是结果很尴尬,乱码了......个人感觉是不是二维数组在子函数调用时出问题了,需要将二维数组当一维数组处理,求大佬解释一下#include<stdio.h>
double AVER1(double a[][5],int m,int n);
double AVER2(double a[][5],int m,int n);
double MAX(double a[][5],int m,int n);
int main()
{
int line,row;
double average1,average2,max;
double a[3][5];
printf("*******************************\n");
printf("This program will finish 3 tasks\n");
printf("*******************************\n");
for(line=0;line<3;line++)
{
for(row=0;row<5;row++)
{
printf("please enter %d line,%d row number:",line+1,row+1);
scanf("%f",&a[line][row]);
while(getchar()!='\n')
continue;
}
}
average1=AVER1(a,3,5);
average2=AVER2(a,3,5);
max=MAX(a,3,5);
printf("average1=%.4f\naverage2=%.4f\nmax=%.4f",average1,average2,max);
return 0;
}
double AVER1(double ar[][5],int m,int n)
{
double aver,sum;
int line,row;
for(line=0;line<m;line++)
{
sum=0;
for(row=0;row<n;row++)
{
sum+=ar[line][row];
}
printf("%d line sum=%.4f,average=%.4f\n",line+1,sum,sum/5);
}
return sum/5;
}
double AVER2(double ar[][5],int m,int n)
{
double aver,sum;
int line,row;
for(line=0,sum=0;line<m;line++)
{
for(row=0;row<n;row++)
{
sum+=ar[line][row];
}
}
printf("all number sum=%.4f,average=%.4f\n",sum,sum/15);
return sum/15;
}
double MAX(double ar[][5],int m,int n)
{
double max;
int line,row;
max=ar[0][0];
for(line=0;line<m;line++)
{
for(row=0;row<n;row++)
{
if(max<ar[line][row])
max=ar[line][row];
else
;
}
}
return max;
}