Introduction

In a square matrix, the diagonal elements are those that extend from the top left corner to the bottom right corner. For example, in a n×nn \times nn×n matrix, the diagonal elements are located at positions (0,0),(1,1),(2,2),…,(n−1,n−1)(0,0), (1,1), (2,2), \ldots, (n-1,n-1)(0,0),(1,1),(2,2),…,(n−1,n−1). The sum of these diagonal elements can be useful in various mathematical applications, including linear algebra and computer graphics.

#include <stdio.h>

#define MAX 10  // Maximum size of the matrix

void inputMatrix(int matrix[MAX][MAX], int size) {
    printf("Enter elements of the matrix (%d x %d):\n", size, size);
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

int sumDiagonal(int matrix[MAX][MAX], int size) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += matrix[i][i];  // Add diagonal elements
    }
    return sum;
}

void printMatrix(int matrix[MAX][MAX], int size) {
    printf("Matrix (%d x %d):\n", size, size);
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int matrix[MAX][MAX];
    int size;

    printf("Enter the size of the matrix: ");
    scanf("%d", &size);

    inputMatrix(matrix, size);
    
    printf("Original Matrix:\n");
    printMatrix(matrix, size);
    
    int diagonalSum = sumDiagonal(matrix, size);
    printf("Sum of diagonal elements: %d\n", diagonalSum);

    return 0;
}

Explanation

  1. Includes and Defines: The program includes the stdio.h header for input/output functions and defines a constant MAX for the maximum size of the matrix.
  2. Input Function: The inputMatrix function prompts the user to enter the elements of a square matrix of size size×sizesize \times sizesize×size.
  3. Sum Function: The sumDiagonal function calculates the sum of the diagonal elements by iterating through the matrix and adding the elements located at positions (i,i)(i, i)(i,i).
  4. Print Function: The printMatrix function displays the matrix in a formatted manner.
  5. Main Function: In main, the program:
    • Declares a matrix.
    • Asks for the size of the matrix.
    • Calls the input, print, and sum functions to display the results.

Input

The user is prompted to enter the size of the matrix, followed by the matrix elements. Example input:

Enter the size of the matrix: 3
Enter elements of the matrix (3 x 3):
1 2 3
4 5 6
7 8 9

Output

The program will output the original matrix and the sum of its diagonal elements. For the example input, the output will be:

Original Matrix:
1 2 3 
4 5 6 
7 8 9 

Sum of diagonal elements: 15

Conclusion

This C program effectively demonstrates how to find the sum of the diagonal elements in a square matrix. This operation is fundamental in various mathematical contexts and serves as a useful exercise for understanding matrix manipulation in C. The code can be easily modified to handle different matrix sizes or data types, making it adaptable for various applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights