C Program to Check Matrix Symmetry

1. Introduction

Matrix symmetry is an interesting concept in linear algebra. A matrix is termed symmetric if it is equal to its transpose. In other words, the matrix (A) is symmetric if (A = A^T). In this guide, we'll walk through a C program that checks if a given matrix is symmetric or not.

2. Program Overview

The C program will:

1. Ask the user for the dimensions of the matrix.

2. Ensure the matrix is square (because only square matrices can be symmetric).

3. Take the elements of the matrix as input from the user.

4. Check if the matrix is symmetric.

5. Display the result to the user.

3. Code Program

#include <stdio.h>

int main() {
    int matrix[10][10], transpose[10][10];
    int rows, cols;
    int i, j, isSymmetric = 1;

    // Asking user to input the dimensions of the matrix
    printf("Enter the number of rows and columns of the matrix: ");
    scanf("%d%d", &rows, &cols);

    // Only square matrices can be symmetric
    if (rows != cols) {
        printf("Matrix is not square, thus cannot be symmetric.");
        return 0;
    }

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

    // Finding the transpose of the matrix
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }

    // Checking if matrix is symmetric or not
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            if(matrix[i][j] != transpose[i][j]) {
                isSymmetric = 0;
                break;
            }
        }
    }

    // Displaying the result
    if(isSymmetric)
        printf("The matrix is symmetric.");
    else
        printf("The matrix is not symmetric.");

    return 0;
}

Output:

Enter the number of rows and columns of the matrix: 3 3
Enter elements of the matrix:
1 2 3
2 4 5
3 5 6
The matrix is symmetric.

4. Step By Step Explanation

1. Variable Declaration:

- The matrix matrix and its transpose transpose are declared.

- Other variables to store dimensions and check symmetry are also declared.

2. Getting Dimensions and Input:

- The program prompts the user to provide the dimensions.

- A check is performed to ensure the matrix is square.

- The user is then prompted to input the elements of the matrix.

3. Finding Transpose:

- The program calculates the transpose of the matrix. Transpose of a matrix is obtained by swapping rows with columns.

4. Checking Symmetry:

- The original matrix and its transpose are compared. If they are the same, the matrix is symmetric.

5. Displaying the Result:

- The program then outputs whether the matrix is symmetric or not. 

This check for matrix symmetry is used in various algorithms and applications, especially in physics and engineering, where symmetric matrices often have special properties that can be exploited.

Comments