![](https://i-blog.csdnimg.cn/blog_migrate/30100bd3f4217781052868f11d9bf7e7.png)
矩阵主对角线元素之和
Here is the simple C program to find sum of elements above and below main diagonal of matrix. The matrix should be square matrix.
这是一个简单的C程序,用于查找矩阵主对角线上方和下方的元素之和。 矩阵应为方矩阵。
#include<stdio.h>
int main()
{
int i,j,m,n,d1=0,d2=0,a[5][5];
printf("How many rows and columns:");
scanf("%d%d",&m,&n);
printf("Enter matrix elements:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
scanf("%d",&a[i][j]);
if(j>i)
d1+=a[i][j];
else
if(i>j)
d2+=a[i][j];
}
printf(\n"Sum of elements above the diagonal=%d\n",d1);
printf("Sum of elements below the diagonal=%d",d2);
return 0;
}
Output
输出量
How many rows and columns:3 3 Enter matrix elements: 4 12 7 4 5 2 1 0 3
多少行和多少列:3 3 输入矩阵元素: 4 12 7 4 5 2 1 0 3
Sum of elements above the diagonal=21 Sum of elements below the diagonal=5
对角线以上的元素总数= 21 对角线以下的元素总数= 5
翻译自: https://www.thecrazyprogrammer.com/2013/03/c-program-to-find-sum-of-elements-above.html
矩阵主对角线元素之和