C程序 计算矩阵对角线之和

C程序 计算矩阵对角线之和

在这里,我们将用以下3种方法计算矩阵的对角线之和。

1.使用条件语句
2.在使用条件语句的同时从使用r中获取自定义输入
3.使用功能

我们将在上述所有方法中保持相同的输入,并得到相应的输出。

输入:

The matrix is 
1 2 3
4 5 6
7 8 9

输出:

Main diagonal elements sum is = 15
Off-diagonal elements sum is = 15 

解释: 主对角线是1,5,和9。所以,主对角线的总和是1+5+9=15。非对角线是3、5和7。所以,主对角线之和是3+5+7=15。

方法1:条件性语句

// C Program to demonstrate the
// Sum of Diagonals of a Matrix
#include <stdio.h>
  
int main()
{
  
    int i, j, m = 3, n = 3, a = 0, sum = 0;
    
    // input matrix
    int matrix[3][3]
        = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    
    // if both rows and columns are equal then it is
    // possible to calculate diagonal sum
    if (m == n) {
        
        // printing the input matrix
        printf("The matrix is \n");
        
        // iterates number of rows
        for (i = 0; i < m; ++i) {
            
            // iterates number of columns
            for (j = 0; j < n; ++j) {
                printf(" %d", matrix[i][j]);
            }
            printf("\n");
        }
        for (i = 0; i < m; ++i) {
            
            // calculating the main diagonal sum
            sum = sum + matrix[i][i];
            
            // calculating the off diagonal sum
            a = a + matrix[i][m - i - 1];
        }
        
        // printing the result
        printf("\nMain diagonal elements sum is = %d\n", sum);
        printf("Off-diagonal elements sum is = %d\n", a);
    }
    else
        // if both rows and columns are not equal then it is
        // not possible to calculate the sum
        printf("not a square matrix\n");
    return 0;
}

输出

The matrix is 
 1 2 3
 4 5 6
 7 8 9

Main diagonal elements sum is = 15
Off-diagonal elements sum is   = 15

方法2:从用户那里获取输入矩阵

// C Program to Demonstrate the Sum of Diagonals
// of a Matrix by taking input from the user
#include <stdio.h>
  
int main()
{
  
    int i, j, m = 3, n = 3, a = 0, sum = 0;
    int matrix[10][10];
    
    // if both rows and columns are equal then it is
    // possible to calculate diagonal sum
    if (m == n) {
        
        // entering the coefficients of the matrix
        for (i = 0; i < m; ++i) {
            
            for (j = 0; j < n; ++j) {
                
                scanf("%d", &matrix[i][j]);
            }
        }
        // printing the input matrix
        printf("The matrix is \n");
        
        // iterates number of rows
        for (i = 0; i < m; ++i) {
            
            // iterates number of columns
            for (j = 0; j < n; ++j) {
                printf(" %d", matrix[i][j]);
            }
            printf("\n");
        }
        for (i = 0; i < m; ++i) {
            
            // calculating the main diagonal sum
            sum = sum + matrix[i][i];
            
            // calculating the off diagonal sum
            a = a + matrix[i][m - i - 1];
        }
        // printing the result
        printf("\nMain diagonal elements sum is = %d\n",sum);
        printf("Off-diagonal elements sum is = %d\n", a);
    }
    else
        // if both rows and columns are not equal then it is
        // not possible to calculate the sum
        printf("not a square matrix\n");
    return 0;
}

输出:

The matrix is 
1 2 3
4 5 6
7 8 9
Main diagonal elements sum is = 15
Off-diagonal elements sum is = 15

方法3:使用函数

// C Program to demonstrate the Sum of Diagonals
// of a Matrix by using functions
#include <stdio.h>
const int max = 10;
  
// Declaration of function
int diagonal_sum(int m, int n, int matrix[][max])
{
    int i, j, a = 0, sum = 0;
    
    // if both rows and columns are equal then it is
    // possible to calculate diagonal sum
    if (m == n) {
        
        // printing the input matrix
        printf("The matrix is \n");
        
        // iterates number of rows
        for (i = 0; i < m; ++i) {
            
            // iterates number of columns
            for (j = 0; j < n; ++j) {
                printf(" %d", matrix[i][j]);
            }
            printf("\n");
        }
        for (i = 0; i < m; ++i) {
            
            // calculating the main diagonal sum
            sum = sum + matrix[i][i];
            
            // calculating the off diagonal sum
            a = a + matrix[i][m - i - 1];
        }
        
        // printing the result
        printf("\nMain diagonal elements sum is = %d\n", sum);
        printf("Off-diagonal elements sum is = %d\n", a);
    }
    else
        // if both rows and columns are not equal then it is
        // not possible to calculate the sum
        printf("not a square matrix\n");
}
  
int main()
{
  
    int m = 3, n = 3;
    
    // input matrix
    int matrix[][10] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    
    diagonal_sum(m, n, matrix);
  
    return 0;
}

输出

The matrix is 
 1 2 3
 4 5 6
 7 8 9

Main diagonal elements sum is = 15
Off-diagonal elements sum is = 15

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

C语言 实例