矩阵主对角线元素之和_C程序,用于查找矩阵主对角线上方和下方的元素之和

这是一个C语言程序,用于求解方矩阵主对角线上方和下方元素的和。用户输入矩阵大小及元素后,程序会分别计算并显示这两个和。
摘要由CSDN通过智能技术生成

矩阵主对角线元素之和

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

矩阵主对角线元素之和

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值