请教下,关于多维数组的scanf输入

killme2008 2007-07-31 03:33:52
这是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;
}
...全文
222 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
killme2008 2007-07-31
  • 打赏
  • 举报
回复
非常感谢,我还是不大习惯以指针角度去看待数组,还要多多练习
jixingzhong 2007-07-31
  • 打赏
  • 举报
回复
修正问题, 输入格式不正确:

#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[0],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[0],cols);
double a_b=average1(data[1],cols);
double a_c=average1(data[2],cols);
printf("\naverage 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("%lf",arr+i); //输入格式, %lf
printf("Enter next double:\n");
}
}

double average1(double *arr,int r)
{
int i;
double result=0.0;
// printf("yes %f\n",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;
}
jixingzhong 2007-07-31
  • 打赏
  • 举报
回复
#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[0],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[0],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;
}


修正语法问题,
注意函数参数, 类型不匹配 ...
killme2008 2007-07-31
  • 打赏
  • 举报
回复
这样肯定是不行的,arr+i指向的是第i行,而我这里关心的是第i列才对
我怀疑是我的函数原型声明方式有问题
syy64 2007-07-31
  • 打赏
  • 举报
回复
void getinput(double * arr,int r)
{
int i;
for(i=0;i<r;i++)
{
scanf("%g",*(arr+i));
printf("Enter next double:\n");
}
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧