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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/CompoundTypesGroup(["`Compound Types`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/CompoundTypesGroup -.-> c/arrays("`Arrays`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/comments -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/variables -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/data_types -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/operators -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/if_else -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/for_loop -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/arrays -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/user_input -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/memory_address -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/pointers -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/function_parameters -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} c/function_declaration -.-> lab-123215{{"`Check Square Matrix Symmetry`"}} end

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