C程序设计的求二维数组各列各行的和与总和

2024-11-22 20:47:35
推荐回答(1个)
回答1:

#include
main(){
int m,n;
int i,j;
int line_sum=0,row_sum=0;
int total=0;

int a[100][100];
printf("输入行数:");
scanf("%d",&m);
printf("输入列数:");
scanf("%d",&n);
//输入矩阵
printf("输入矩阵:\n");
for(i=0;i for(j=0;j printf("a[%d][%d](输入后按回车键):",i,j);
scanf("%d",&a[i][j]);
}

//返回行的和
for(i=0;i line_sum=0;
for(j=0;j line_sum+=a[i][j];
total+=line_sum;
printf("第%5d行的和:%5d",i+1,line_sum);
printf("\n");
}
printf("*******************\n");
//返回列的值
for(i=0;i row_sum=0;
for(j=0;j row_sum+=a[j][i];
total+=row_sum;
printf("第%5d列的和:%5d",i+1,row_sum);
printf("\n");
}
printf("*******************\n");
//总和
printf("合计:%d\n",total);
return 0;
}