C++ Program to Check if a Matrix is Symmetric or Skew-Symmetric

This is a C++ Program to Find if a Matrix is Symmetric or Skew-Symmetric.

Problem Description

The program takes an array and checks whether it is a symmetric or skew-symmetric matrix. If a square matrix is equal to its transpose, it is a symmetric matrix. If transpose of a matrix is equal to negative of itself, then it is a skew symmetric matrix.

Problem Solution

1. The program takes a matrix.
2. If the matrix is equal to its transpose, then it’s a symmetric matrix.
3. Else if it’s transpose is equal to negative of itself, then matrix is skew-symmetric.
4. Else it is neither.
5. The result is printed.
6. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find if a Matrix is Symmetric or Skew-Symmetric. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int A[10][10], i, j, m, n, x = 0, y = 0;
  6.     cout << "Enter the number of rows and columns : ";
  7.     cin >> m >> n;
  8.     cout << "Enter the matrix elements : ";
  9.     for (i = 0; i < m; i++)
  10.         for (j = 0; j < n; j++)
  11.             cin >> A[i][j];
  12.     for (i = 0; i < m; i++)
  13.     {
  14.         for( j = 0; j < n; j++)
  15.         {
  16.             if (A[i][j] != A[j][i])
  17.                 x = 1;
  18.             else if (A[i][j] == -A[j][i])
  19.                     y = 1;
  20.         }
  21.     }
  22.     if (x == 0)
  23.         cout << "The matrix is symmetric.\n ";
  24.     else if (y == 1)
  25.         cout << "The matrix is skew symmetric.\n ";
  26.     else
  27.         cout << "It is neither symmetric nor skew-symmetric.\n ";
  28.     for (i = 0; i < m; i++)
  29.         {
  30.             for (j = 0; j < n; j++)	
  31.                 cout << A[i][j] << " ";
  32.             cout << "\n ";
  33.         }
  34.     return 0;
  35. }
Program Explanation

1. The user is asked to enter number of rows and columns of the matrix.
2. The elements of the matrix are asked to enter and stored in ‘A’. Variables ‘x’ and ‘y’ are initialized as 0.
3. If the matrix is not equal to its transpose, a temporary variable ‘x’ is assigned 1.
4. Else if the negative of matrix is equal to its transpose, a temporary variable ‘y’ is assigned 1.
5. If x is equal to 0, then matrix is symmetric. Else if y is equal to 1, matrix is skew-symmetric.
6. If neither of the conditions satisfy, the matrix is neither symmetric nor skew-symmetric.
7. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter the number of rows and columns : 2 2
Enter the matrix elements : 10 20 20 10
The matrix is symmetric.
 10 20
 20 10
 
Case 2 :
Enter the matrix elements : 0 1 -2 -1 0 3 2 -3 0
The matrix is skew symmetric.
 0 1 -2
 -1 0 3 
 2 -3 0
 
Case 3 :
Enter the number of rows and columns : 1 3
Enter the matrix elements : 1 0 1
It is neither symmetric nor skew-symmetric.
 1 0 1

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.