C++ Program to Check Symmetric Matrix

C++ Program to Check Symmetric Matrix

In linear algebra, a symmetric matrix is a square matrix that is equal to its transpose. In other words, the elements across the main diagonal are mirrored. Checking whether a matrix is symmetric is an essential concept in mathematics, physics simulations, and computer graphics. Learning how to implement a C++ program to check a symmetric matrix helps beginners practice arrays, loops, and conditional logic while reinforcing the understanding of matrix properties. In this article, we will explore multiple programs, including user input and predefined matrices, to demonstrate symmetry checking clearly.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Check Symmetric Matrix Using User Input

This program allows the user to input a square matrix and checks whether it is symmetric by comparing elements across the main diagonal.

#include <iostream>

using namespace std;

int main() {

    int n;

    cout << "Enter the size of the square matrix: " << endl;
    cin >> n;

    int matrix[n][n];

    cout << "Enter elements of the matrix:" << endl;

    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            cin >> matrix[i][j];

    bool symmetric = true;

    for(int i = 0; i < n; i++){

        for(int j = 0; j < n; j++){

            if(matrix[i][j] != matrix[j][i]){
                symmetric = false;
                break;
            }

        }

        if(!symmetric) break;

    }

    if(symmetric)
        cout << "The matrix is symmetric." << endl;
    else
        cout << "The matrix is not symmetric." << endl;

    return 0;

}

In this program, the user enters a square matrix. Nested loops are used to compare each element with its transposed counterpart. If all corresponding elements are equal, the matrix is symmetric. This program helps beginners understand how array indices can be manipulated to compare elements across the diagonal.

Program 2: Check Symmetric for a Predefined 3×3 Matrix

For beginners who want to focus on logic without inputting values, this program uses a predefined 3×3 matrix.

#include <iostream>

using namespace std;

int main() {

    int matrix[3][3] = {{1, 2, 3}, {2, 5, 6}, {3, 6, 9}};

    bool symmetric = true;

    for(int i = 0; i < 3; i++){

        for(int j = 0; j < 3; j++){

            if(matrix[i][j] != matrix[j][i]){
                symmetric = false;
                break;
            }

        }

        if(!symmetric) break;

    }

    cout << "Matrix:" << endl;

    for(int i = 0; i < 3; i++){

        for(int j = 0; j < 3; j++)
            cout << matrix[i][j] << " ";

        cout << endl;

    }

    if(symmetric)
        cout << "The matrix is symmetric." << endl;
    else
        cout << "The matrix is not symmetric." << endl;

    return 0;

}

This program demonstrates symmetry checking without requiring user input. Beginners can visually see that elements mirrored across the diagonal are equal, making it easier to understand the concept of symmetric matrices.

Program 3: Colorful Symmetric Check for a 3×3 Predefined Matrix

This version uses color formatting to make it more engaging. The original matrix is displayed in cyan, and a message about symmetry is highlighted in yellow.

#include <iostream>

using namespace std;

int main() {

    int matrix[3][3] = {{4, 1, 7}, {1, 5, 2}, {7, 2, 6}};

    bool symmetric = true;

    for(int i = 0; i < 3; i++){

        for(int j = 0; j < 3; j++){

            if(matrix[i][j] != matrix[j][i]){
                symmetric = false;
                break;
            }

        }

        if(!symmetric) break;

    }

    cout << "\033[1;36mMatrix:\033[0m" << endl;

    for(int i = 0; i < 3; i++){

        for(int j = 0; j < 3; j++)
            cout << "\033[1;36m" << matrix[i][j] << " \033[0m";

        cout << endl;

    }

    if(symmetric)
        cout << "\033[1;33mThe matrix is symmetric.\033[0m" << endl;
    else
        cout << "\033[1;33mThe matrix is not symmetric.\033[0m" << endl;

    return 0;

}

The colorful display helps beginners visually associate the matrix elements with the symmetry check. Using ANSI escape codes, the matrix and messages are highlighted to make the learning process interactive and easier to follow.

Program 4: Colorful Symmetric Check for a 4×4 Predefined Matrix

This program checks if a 4×4 predefined matrix is symmetric and displays the matrix in cyan and the result in yellow for better visualization.

#include <iostream>

using namespace std;

int main() {

    int matrix[4][4] = {
        {1, 2, 3, 4},
        {2, 5, 6, 7},
        {3, 6, 8, 9},
        {4, 7, 9, 10}
    };

    bool symmetric = true;

    for(int i = 0; i < 4; i++){

        for(int j = 0; j < 4; j++){

            if(matrix[i][j] != matrix[j][i]){
                symmetric = false;
                break;
            }

        }

        if(!symmetric) break;

    }

    // Display the original 4x4 matrix in cyan
    cout << "\033[1;36mOriginal 4x4 Matrix:\033[0m" << endl;

    for(int i = 0; i < 4; i++){

        for(int j = 0; j < 4; j++)
            cout << "\033[1;36m" << matrix[i][j] << " \033[0m";

        cout << endl;

    }

    // Display the symmetry result in yellow
    if(symmetric)
        cout << "\033[1;33mThe matrix is symmetric.\033[0m" << endl;
    else
        cout << "\033[1;33mThe matrix is not symmetric.\033[0m" << endl;

    return 0;

}

The program uses nested loops to compare each element of the matrix with its corresponding element across the diagonal. If all elements match their transposed counterparts, the matrix is symmetric. The ANSI escape codes make the matrix cyan and the result yellow, helping beginners visually track the elements and clearly see the symmetry check.

Frequently Asked Questions (FAQ)

Q1: What is a symmetric matrix?
A1: A symmetric matrix is a square matrix that is equal to its transpose. In other words, the element at row i, column j is equal to the element at row j, column i.

Q2: Can non-square matrices be symmetric?
A2: No. Only square matrices (same number of rows and columns) can be symmetric.

Q3: Why is symmetry important?
A3: Symmetric matrices are used in physics, computer graphics, and solving systems of linear equations because they have special properties, such as real eigenvalues.

Q4: How does the program check symmetry?
A4: It compares each element with its corresponding transposed element. If all pairs are equal, the matrix is symmetric.

Q5: Why use colorful output?
A5: Colors make it easier for beginners to identify the matrix and the result, making the learning experience engaging.

Conclusion

Checking if a matrix is symmetric in C++ is an important exercise to strengthen understanding of arrays, loops, and conditional statements. By practicing with user input, predefined matrices, and colorful displays, beginners can visually understand symmetry and gain confidence in array manipulation. Regular practice with symmetric matrices prepares learners for advanced topics like matrix decomposition, eigenvalues, and linear transformations.

Additional & References

Beginners are encouraged to explore concepts such as skew-symmetric matrices, upper and lower triangular matrices, and matrix transposition for a deeper understanding of linear algebra in C++.

Scroll to Top