Check Square Matrix Symmetry

CCBeginner
Practice Now

Introduction

In this lab, we will learn how to check whether a square matrix is symmetric or not using the C programming language. A square matrix is said to be symmetric if it's equal to it's transpose. We will find the transpose of the matrix and compare it with the original matrix to check its symmetry.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Declare the Variables and Input the Matrix

First, we need to declare the variables and input the matrix from the user. We will use nested loops to input the matrix. To input the matrix from the user, we first ask the user for the dimension of the matrix and store it in 'n'. Then we input each element of the matrix using a loop.

#include <stdio.h>

int main() {

    int c, d, a[10][10], b[10][10], n;
    printf("\nEnter the dimension of the matrix: \n\n");
    scanf("%d", &n);

    printf("\nEnter the %d elements of the matrix: \n\n", n * n);
    for (c = 0; c < n; c++)
        for (d = 0; d < n; d++)
            scanf("%d", &a[c][d]);

    return 0;
}

Find the Transpose of the Matrix

The transpose of the matrix is achieved by exchanging the indices of rows and columns. We use nested loops to find out the transpose of the matrix. To find the transpose of the matrix, we use two loops. The first loop iterates over the rows, and the second loop iterates over the columns.

// finding transpose of a matrix and storing it in b[][]
for (c = 0; c < n; c++)
    for (d = 0; d < n; d++)
        b[d][c] = a[c][d];

Check Matrix Symmetry

In this step, we will check whether the original matrix is the same as its transpose or not. We use nested loops to check this. To check the symmetry of the matrix, we use two loops. The first loop iterates over the rows, and the second loop iterates over the columns.

// checking if the original matrix is the same as its transpose
for (c = 0; c < n; c++)
    for (d = 0; d < n; d++)
        if (a[c][d] != b[c][d]) {
            printf("\n\nMatrix is not Symmetric\n\n");
            exit(0); // a system defined method to terminate the program
        }

rint Results

After checking that the matrix is symmetric, the program displays a message confirming this. The program will terminate if the matrix is not symmetric.

// If the program is not terminated yet, it means the matrix is symmetric
printf("\n\nMatrix is Symmetric\n\n");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");

Summary

In this lab, we learned how to check whether a square matrix is symmetric or not using the C programming language. We used nested loops to input and find the transpose of the matrix, then checked whether the original matrix is the same as its transpose or not. If the two matrices were the same, the program displayed a message stating the matrix is symmetric.

Other C Tutorials you may like