一整型二维数组a[4][5],从键盘上输入数据并求a数组中最大,最小元素值及所有元素的平均值。要求

2024-12-01 03:24:03
推荐回答(1个)
回答1:

#include
void fun(int **a, int NR, int NC, int *da, int *xiao, float *av){
int i,j;
*da = a[0][0];
*xiao = a[0][0];
*av=0;
for (j=0;jfor (i=0;i{
if (*da if (*xiao > a[j][i]) *xiao = a[j][i];
*av= *av+a[j][i];
}
*av = (*av /NR) /NC;
}

int main(){
int **a;
int NR=4,NC=5;
int i,j;
int da,xiao;
float av;

a = (int **) malloc(sizeof(int *) * NR);
for (j=0;ja[j] = (int *) malloc(sizeof(int) * NC);
}

printf("input %d x %d array data:\n",NR,NC);
for (j=0;jscanf("%d",&a[j][i]);
}
fun( &a[0], NR, NC, &da, &xiao, &av);
printf("max=%d min=%d mean=%g\n",da,xiao,av);
return 0;
}
例子:
input 4 x 5 array data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
max=20 min=1 mean=10.5