考虑到这道题的性质,用一维数组运算速度更快(原理就不说了,要想问再留言)
int arry1[5]={1,3,5,7,9};
int arry2[5]={2,4,6,8,10};
int arry3[5]={3,5,8,7,6};
int sum_row[3]={0*3};/*一行元素之和*/
int sum_column[5]={0*8};/*一列元素之和*/
for(int i=0;i<5;i++)
{
sum_row[0]+=arry1[i];
sum_row[1]+=arry2[i];
sum_row[2]+=arry3[i];
sum_column[i]=arry1[i]+arry2[i]+arry2[i];
printf("第%d列和为%d",i,sum_column[i]);
}
for(int i=0;i<3;i++)
{
printf("第%d行和为%d",i,sum_row[i]);
}
#include
void main(void)
{
int a[3][5] = {{1,3,5,7,9}, {2, 4, 6, 8, 10}, {3, 5, 8, 7, 6}};
int i, j, r_sum[3], c_sum[5];
for (i=0; i<3; i++)// 各行的和
{
r_sum[i] = 0;
for (j=0; j<5; j++)
{
r_sum[i] += a[i][j];
}
printf("第%d行的和为:%d\n", i, r_sum[i]);
}
for (j=0; j<5; j++)// 各列的和
{
c_sum[j] = 0;
for (i=0; i<3; i++)
{
c_sum[j] += a[i][j];
}
printf("第%d列的和为:%d\n", j, c_sum[j]);
}
}