Write a Program to Find the Sum of the Elements on the Main Diagonal of a Matrix in C

Write a Program to Find the Sum of the Elements on the Main Diagonal of a Matrix in C

In this post, we will explore how to write a C program to calculate the sum of the elements along the main diagonal of a matrix. The main diagonal of a matrix refers to the elements where the row index is equal to the column index.

1. Understanding the Main Diagonal

The main diagonal of a matrix consists of the elements where the row index and column index are equal. For example, in a 3x3 matrix:

  [a11, a12, a13]
  [a21, a22, a23]
  [a31, a32, a33]
  

The main diagonal elements are a11, a22, and a33. The task is to sum these elements together.

2. Writing the Program

Below is a C program to find the sum of the main diagonal elements of a square matrix.

Program to Find the Sum of the Main Diagonal Elements

#include <stdio.h>

int main() {
    int n, sum = 0;
    int matrix[10][10];

    // Input the size of the square matrix
    printf("Enter the size of the matrix (n x n): ");
    scanf("%d", &n);

    // Input elements of the matrix
    printf("Enter the elements of the matrix:\n");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("Element at [%d][%d]: ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }

    // Calculate the sum of the main diagonal elements
    for (int i = 0; i < n; i++) {
        sum += matrix[i][i];  // Only adding the elements where row index equals column index
    }

    // Output the result
    printf("The sum of the elements on the main diagonal is: %d\\n", sum);

    return 0;
}

Explanation of the Code

Here's a breakdown of the code:

  • The program first prompts the user to input the size of the matrix (n) and the elements of the matrix.
  • It then loops through the matrix to calculate the sum of the diagonal elements where the row index equals the column index.
  • Finally, the program prints the sum of the main diagonal elements.

3. Example Output

Here’s an example of how the program works:

Enter the size of the matrix (n x n): 3
Enter the elements of the matrix:
Element at [0][0]: 1
Element at [0][1]: 2
Element at [0][2]: 3
Element at [1][0]: 4
Element at [1][1]: 5
Element at [1][2]: 6
Element at [2][0]: 7
Element at [2][1]: 8
Element at [2][2]: 9

The sum of the elements on the main diagonal is: 15
  

4. Common Mistakes to Avoid

  • Non-Square Matrix: Ensure that the matrix is square (same number of rows and columns) since only square matrices have a main diagonal.
  • Incorrect Indexing: When summing the elements, ensure you're accessing the diagonal elements by using matrix[i][i].

5. Conclusion

In this tutorial, we discussed how to write a C program to find the sum of the main diagonal elements of a matrix. The main diagonal is a simple yet important concept in matrix operations. By mastering basic operations like this, you can move on to more advanced matrix manipulations in future programs.

0 تعليقات

إرسال تعليق

Post a Comment (0)

أحدث أقدم