# Find a 5 respectively × 5 the sum of all rows, columns and elements on two diagonal lines of the matrix. (the input data is inconsistent with the output data)

2022-05-15 04:47:31

###### Find one respectively 5×5 The sum of all rows of the matrix 、 The sum of the columns 、 The sum of the elements on the two diagonals .

Input data , Inconsistent after output

``#include<stdio.h>#define M 2int main(){    int i,j,a[i][j],sum;    printf(" Please enter each element of the matrix ：\n");    for(i=0;i<M;i++){        for(j=0;j<M;j++){            scanf("%d",&a[i][j]);        }    }    printf(" The input matrix elements are ：\n");    for(i=0;i<M;i++){        for(j=0;j<M;j++){            printf("%d,",a[i][j]);        }        printf("\n");    }}``

Please enter each element of the matrix ：
4 1 4 1
The input matrix elements are ：
4,4,
4,1,

Direct calculation with formula ：

``#include<stdio.h>#define M 2int main(){    int i,j,a[i][j],sum,sum1,sum2;    sum=sum1=sum2=0;    printf(" Please enter each element of the matrix ：\n");    for(i=0;i<M;i++){        for(j=0;j<M;j++){            scanf("%d",&a[i][j]);        }    }    printf(" The input matrix elements are ：\n");    for(i=0;i<M;i++){        for(j=0;j<M;j++){            printf("%d,",a[i][j]);        }        printf("\n");    }    for(i=0;i<M;i++){        sum=0;        for(j=0;j<M;j++){            sum+=a[i][j];        }        a[i][j]=sum;        printf("%d",a[i][j]);    }    for(i=0,j=0;i<M;i++,j++)            sum1+=a[i][j];        for(i=0,j=0;i>M;i--,j++)            sum2+=a[i][j];        printf(" The sum of the elements on the main diagonal is :%d\n The sum of the elements on the diagonal is :%d\n",sum1,sum2);}``

give the result as follows ：
Please enter each element of the matrix ：
1 1 1 1
The input matrix elements are ：
1,1,
1,1,
23 The sum of the elements on the main diagonal is :3
The sum of the elements on the diagonal is :0