请教下,关于多维数组的scanf输入
这是c primer plus第10章的习题12,读取一个3行5列的数组,然后计算平均值等,我的写法如下,我为了练习指针,尽量都采用指针的传参形式,可我的写法似乎有问题,读取总是错误,请各位指点下,谢谢。
#include<stdio.h>
#define rows 3
#define cols 5
void getinput(double *,int);
double average1(double *,int);
double average2(double (* ar)[5],int r);
double get_max(double (* ar)[5],int r);
int main(void)
{
double data[rows][cols];
printf("Please enter the first dataset:\n");
getinput(data,cols);
printf("Enter the second data set:\n");
getinput(data+1,cols);
printf("Enter the third data set:\n");
getinput(data+2,cols);
double a_a=average1(data,cols);
double a_b=average1(data+1,cols);
double a_c=average1(data+2,cols);
printf("average of a is %.2f\n",a_a);
printf("average of b is %.2f\n",a_b);
printf("average of c is %.2f\n",a_c);
printf("average of all is %.2f\n",average2(data,rows));
printf("max of all is %.2f\n",get_max(data,rows));
return 0;
}
void getinput(double * arr,int r)
{
int i;
for(i=0;i<r;i++)
{
scanf("%g",*arr+i);
printf("Enter next double:\n");
}
}
double average1(double *arr,int r)
{
int i;
double result=0.0;
printf("yes %f",arr[0]);
for(i=0;i<r;i++)
result+=*(arr+i);
return result/r;
}
double average2(double (* ar)[5],int r)
{
double result=0.0;
int i,j;
for(i=0;i<r;i++)
for(j=0;j<5;j++)
result+=*(*(ar+i)+j);
return result/(r*cols);
}
double get_max(double (* ar)[5],int r)
{
double max;
int i,j;
for(i=0;i<r;i++)
for(j=0;j<cols;j++)
if(*(*(ar+i)+j)>max)
max=*(*(ar+i)+j);
return max;
}