C Program To Check whether Matrix is Skew Symmetric or not?

CServer Side ProgrammingProgramming

Square Matrix A is said to be skew-symmetric if aij=−aji for all i and j. In other words, we can say that matrix A is said to be skew-symmetric if transpose of matrix A is equal to negative of Matrix A i.e (AT=−A).

Note that all the main diagonal elements in the skew-symmetric matrix are zero.

lets take an example of a matrix

A= |0 -5 4|
   |5 0 -1|
   |-4 1 0|

It is skew-symmetric matrix because aij=−aji for all i and j. Example, a12 = -5 and a21=5 which means a12=−a21. Similarly, this condition holds true for all other values of i and j.

We can also verify that Transpose of Matrix A is equal to negative of matrix A i.e AT=−A.

AT= |0 5 -4|
    |-5 0 1|
    |4 -1 0|
and
A= |0 -5 4|
   |5 0 -1|
   |-4 1 0|

We can clearly see that AT=−A which makes A skew-symmetric matrix.

Input:
Enter the number of rows and columns: 2 2
Enter the matrix elements: 10 20 20 10
Output:
The matrix is symmetric.
10 20
20 10

Explanation

If the matrix is equal to its transpose, then it’s a symmetric matrix.

Else if it’s transpose is equal to the negative of itself, then the matrix is skew-symmetric. Else it is neither. The result is printed accordingly

The process to check for symmetry of a matrix

  • The user is asked to enter a number of rows and columns of the matrix.

  • The elements of the matrix are asked to enter and store in ‘A’. Variables ‘x’ and ‘y’ are initialized as 0.

  • If the matrix is not equal to its transpose, a temporary variable ‘x’ is assigned 1.

  • Else if the negative of the matrix is equal to its transpose, a temporary variable ‘y’ is assigned 1.

  • If x is equal to 0, then the matrix is symmetric. Else if y is equal to 1, the matrix is skew-symmetric.

  • If neither of the conditions satisfies, the matrix is neither symmetric nor skew-symmetric.

  • The result is then printed.

Example

#include<iostream>
using namespace std;
int main () {
   int A[10][10], i, j, m, n, x = 0, y = 0;
   cout << "Enter the number of rows and columns : ";
   cin >> m >> n;
   cout << "Enter the matrix elements : ";
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
   cin >> A[i][j];
   for (i = 0; i < m; i++) {
      for( j = 0; j < n; j++) {
         if (A[i][j] != A[j][i])
            x = 1;
         else if (A[i][j] == -A[j][i])
            y = 1;
      }
   }
   if (x == 0)
      cout << "The matrix is symmetric.\n ";
   else if (y == 1)
      cout << "The matrix is skew symmetric.\n ";
   else
      cout << "It is neither symmetric nor skew-symmetric.\n ";
   for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++)
         cout << A[i][j] << " ";
      cout << "\n ";
   }
   return 0;
}
raja
Published on 20-Aug-2019 08:44:46
Advertisements