Program to find the transpose of a given matrix and check whether it is symmetric or not

Write a program to find the transpose of a given matrix and check whether it is symmetric or not

Example:

Input:  matrix={{1,7,3},{7,4,-5},{3,-5,6}}
Output: Symmetric

Approach

C++

#include <bits/stdc++.h>
using namespace std;

bool isSymmetric(vector<vector<int>> matrix)
{
    //squae matrix
    int n = matrix.size();

    int transpose[n][n];

    for (int i = 0i < ni++)
    {
        for (int j = 0j < nj++)
        {
            transpose[j][i] = matrix[i][j];
        }
    }
    for (int i = 0i < ni++)
    {
        for (int j = 0j < nj++)
        {
            if (transpose[i][j] != matrix[i][j])
            {
                return false;
            }
        }
    }
    return true;
}
int main()
{
    //square matrix
    vector<vector<int>> matrix = {{173},
                                  {74, -5},
                                  {3, -56}};

    if (isSymmetric(matrix))
        cout << "Symmetric\n";
    else
        cout << "Not Symmetric\n";

    return 0;
}


No comments:

Post a Comment