RUN
C C++ JAVA PYTHON SQL HTML CSS DSA Robotics AWS CODING INTERVIEW PREPARATION

Made with  & Code

# Symmetric or Skew symmetric matrix in C++

Written By -

Program to find whether a square matrix is

a)symmetric b) skew-symmetric c) none of two

### # Understanding the terms

1. A square matrix is said to be symmetric if its transpose is equal to it:A=A’

Or all elements satisfy the relation:

A[ij] = A[ji]

2. A square matrix is said to be symmetric if its transpose is equal to its negative:
AT = -AOr all elements satisfy the relation:

A[ij] = -A[ji]

3. All diagonal elements of a skew symmetric matrix are zero and for symmetric matrix they can take any value.
4. a b c b e d c d f is the general form of a symmetric matrix.
5. 0 -b -c b 0 -d c d 0 is the general form of a skew-symmetric matrix.

Now in the given question we have make a program that takes a matrix as input and checks if it is symmetric or skew symmetric or none of them.

### # Approaching the problem

We have to check for each element (i,j) of the matrix and compare it with (j,i) and check if they are equal or negative of each other.

To access each element, we will use two nested for loops just like we do when he have to input or print a 2-D array and then we can specify our conditions to check for syymetric and skew symmetric matrices.

We would use a if-else and in if condition we will check for symmetric and inside the else we will check if its skew symmetric or none of above.

### # Algorithm

1. Input from the user the size n of the matrix we wish to check
2. Input a matrix of size nxn from the user with the help of nested for loops and store it in a 2-D array of size nxn.
3. Set a variable ctr to 1.
4. Using a nested for loop check if a[i][j] is equal to a[j][i].if not, set ctr to zero and break out of the loops.
5. Out of the loop check if ctr is still 1 then its symmetric matrix.
6. In the else condition, again set ctr to 1 and using a nested for loop check if a[i][j] is equal to -a[j][i].if not, set ctr to zero and break out of the loops.
7. Out of the loop check if ctr is still 1 then it’s a skew-symmetric matrix and if not then its neither symmetric nor skew symmetric.

Code

``````#include <iostream>
using namespace std;

int main()

{
int n;

cout << "Enter size of square matrix\n"; //inputting size of the matrix

cin >> n;

int a[n][n];

cout << "Enter the matrix row-wise\n";	//inputting the matrix row wise

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

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

{

cin >> a[i][j];
}
}

int ctr = 1;

for (int i = 0; i < n; ++i)	//check for symmetric matrix

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

{

if (a[i][j] != -a[j][i])

{

ctr = 0;

break;
}
}

if (ctr == 0)

break;
}

if (ctr)	//printing if matrix is symmetric

cout << "Matrix is skew-symmetric\n";

else	//checking if skew symmetric matrix

{
ctr = 1;

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

{

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

{

if (a[i][j] != (-a[j][i]))

{

ctr = 0;

break;
}
}

if (ctr == 0)

break;
}

if (ctr)	//printing if matrix is skew symmetric matrix

cout << "Matrix is skew-symmetric\n";

else	//if not then its neither of the two

cout << "Matrix is neither symmetric nor skew-symmetric\n";
}

return 0;

}``````

Output Report Error/ Suggestion

Related Posts:

Online Compilers

CopyRight © 2020

CopyRight © 2020