This is a C++ Program to Find if a Matrix is Symmetric or Skew-Symmetric.
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.
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.
Here is the source code of C++ Program to Find if a Matrix is Symmetric or Skew-Symmetric. The program output is shown below.
#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;
}
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.
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.
- Practice Programming MCQs
- Check Programming Books
- Check C++ Books
- Apply for Computer Science Internship
- Apply for C++ Internship