高分悬赏 C语言数组编程题

2024-11-10 07:32:57
推荐回答(1个)
回答1:

#include "stdio.h"
int sum(int *p){
return p[0]+(p+5)[0]+(p+10)[0]+(p+15)[0];
}
void average(int (*p)[5],double *a,int *s){
int i,j,k;
for(i=0;i<4;i++)
a[i]=(p[i][0]+p[i][1]+p[i][2]+p[i][3]+p[i][4])/5.0;
for(i=0;i<4;i++){
for(k=i,j=k+1;j<4;j++)
if(a[s[k]]>a[s[j]])
k=j;
if(k-i)
j=s[k],s[k]=s[i],s[i]=j;
}
}
void max(int (*p)[5],char (*c)[12]){
int i,j,ii,jj;
for(ii=jj=i=0;i<4;i++)
for(j=0;j<5;j++)
if(p[i][j]>p[ii][jj])
ii=i,jj=j;
printf("The MAX: %d\t%s\t%d\n",p[ii][jj],c[ii],2013+jj);
}
int main(int argc,char *argv[]){
int array[4][5],s[4]={0,1,2,3},i,j;
char city[4][12]={"Beijing","Shanghai","Shenzhen","Guangzhou"};
double ave[4];
printf("Please enter data...\n");
for(i=0;i<4;i++){
printf("%10s: ",city[i]);
for(j=0;j<5;scanf("%d",array[i]+j++));
}
printf("\n");
for(i=0;i<5;i++)
printf("\t%d: %d\n",2013+i,sum(array[0]+i));
average(array,ave,s);
printf("\n     CITY       AVER\n==========================\n");
for(i=0;i<4;i++)
printf("%10s\t%g\n",city[s[i]],ave[s[i]]);
printf("\n");
max(array,city);
return 0;
}

运行样例: