C++ Program to Check Matrix is a Symmetric Matrix

Write a C+ Program to Check Matrix is a Symmetric Matrix with an example. Any matrix can be symmetric if the original matrix is equal to the transpose of that. In this Symmetric Matrix example, first, we transposed the symMat and assigned it to the tMat.

Next, we check whether each item in the symMat matrix does not equal to tMat matrix (if(symMat[rows][columns] != tMat[rows][columns])). If it fails, increment the count value, and the break statement will help the compiler to exit from the loop. If the count value equals 0, then it is a symmetric matrix.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, count = 1;
	
	cout << "\nPlease Enter the Matrix rows and Columns =  ";
	cin >> i >> j;
	
	int symMat[i][j], tMat[i][j];
	
	cout << "\nPlease Enter the Symmetric Matrix Items\n";
	for(rows = 0; rows < i; rows++)	
	{
		for(columns = 0; columns < i; columns++) 
		{
			cin >> symMat[rows][columns];
		}		
	}
	
	for(rows = 0; rows < i; rows++)	
	{
		for(columns = 0; columns < i; columns++) 
		{
			tMat[columns][rows] = symMat[rows][columns];
		}		
	}
 	
	for(rows = 0; rows < i; rows++)
  	{
  		for(columns = 0; columns < j; columns++)
  		{
  			if(symMat[rows][columns] != tMat[rows][columns])
  			{
  				count++;
  				break;
			}
		}
  	}

 	if(count == 1)
  	{
  		cout << "\nThe Matrix you have entered is a Symmetric Matrix";
	}
	else
	{
		cout << "\nThe Matrix you have entered is Not a Symmetric Matrix";
	}  	

 	return 0;
}
C+ Program to Check Matrix is a Symmetric Matrix 1

C+ Program to Check Matrix is a Symmetric Matrix using a While Loop

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, count = 1;
	
	cout << "\nPlease Enter the Matrix rows and Columns =  ";
	cin >> i >> j;
	
	int symMat[i][j], tMat[i][j];
	
	cout << "\nPlease Enter the Symmetric Matrix Items\n";
	for(rows = 0; rows < i; rows++)	
	{
		for(columns = 0; columns < i; columns++) 
		{
			cin >> symMat[rows][columns];
		}		
	}
	rows = 0; 
	while(rows < i)	
	{
		columns = 0; 
		while(columns < i) 
		{
			tMat[columns][rows] = symMat[rows][columns];
			columns++;
		}
		rows++;		
	}
	rows = 0;
	while(rows < i)	
	{
		columns = 0; 
		while(columns < i) 
		{	
  			if(symMat[rows][columns] != tMat[rows][columns])
  			{
  				count++;
  				break;
			}
			columns++;
		}
		rows++;	
  	}

 	if(count == 1)
  	{
  		cout << "\nThe Matrix you have entered is a Symmetric Matrix";
	}
	else
	{
		cout << "\nThe Matrix you have entered is Not a Symmetric Matrix";
	}  	

 	return 0;
}
Please Enter the Matrix rows and Columns =  3 3

Please Enter the Symmetric Matrix Items
10 20 30
20 50 60
30 60 50

The Matrix you have entered is a Symmetric Matrix

In this C++ example, we added extra cout statements to show the iteration-wise row value, symMat[rows][columns] value, tMat[columns][rows] value, and column value.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, count = 1;
	
	cout << "\nPlease Enter the rows and Columns =  ";
	cin >> i >> j;
	
	int symMat[i][j], tMat[i][j];
	
	cout << "\nPlease Enter the Items\n";
	for(rows = 0; rows < i; rows++)	
	{
		for(columns = 0; columns < i; columns++) 
		{
			cin >> symMat[rows][columns];
		}		
	}
	
	for(rows = 0; rows < i; rows++)	
	{
		cout << "\nRow Iteration = " << rows + 1 << ", Row Number = " << rows;
		for(columns = 0; columns < i; columns++) 
		{
			cout << "\nColumn Iteration = " << columns + 1 << ", Column Number = " << 
			  	columns << ", and Row Number = " << rows;
			tMat[columns][rows] = symMat[rows][columns];
			cout<<"\nValue of (symMat[" << rows << "][" << columns << "] = " << symMat[rows][columns] << ". So, tMat[" 
				<< columns << "][" << rows << "]) = " << tMat[columns][rows];
		}		
	}
 	cout <<"\n";
	for(rows = 0; rows < i; rows++)
  	{
  		cout << "\nRow Iteration = " << rows + 1 << ", Row Number = " << rows;
  		for(columns = 0; columns < j; columns++)
  		{
  			cout << "\nColumn Iteration = " << columns + 1 << ", Column Number = " << 
			  	columns << ", and Row Number = " << rows;
			cout<<"\nResult of (symMat[" << rows << "][" << columns << "] != tMat[" 
				<< rows << "][" << columns << "]) = " << (symMat[rows][columns] != tMat[rows][columns]);
  			if(symMat[rows][columns] != tMat[rows][columns])
  			{
  				count++;
  				break;
			}
		}
  	}

 	if(count == 1)
  	{
  		cout << "\nThe Matrix you have entered is a Symmetric";
	}
	else
	{
		cout << "\nThe Matrix you have entered is Not a Symmetric";
	}  	

 	return 0;
}
Please Enter the rows and Columns =  2 2

Please Enter the Items
10 20
20 10

Row Iteration = 1, Row Number = 0
Column Iteration = 1, Column Number = 0, and Row Number = 0
Value of (symMat[0][0] = 10. So, tMat[0][0]) = 10
Column Iteration = 2, Column Number = 1, and Row Number = 0
Value of (symMat[0][1] = 20. So, tMat[1][0]) = 20
Row Iteration = 2, Row Number = 1
Column Iteration = 1, Column Number = 0, and Row Number = 1
Value of (symMat[1][0] = 20. So, tMat[0][1]) = 20
Column Iteration = 2, Column Number = 1, and Row Number = 1
Value of (symMat[1][1] = 10. So, tMat[1][1]) = 10

Row Iteration = 1, Row Number = 0
Column Iteration = 1, Column Number = 0, and Row Number = 0
Result of (symMat[0][0] != tMat[0][0]) = 0
Column Iteration = 2, Column Number = 1, and Row Number = 0
Result of (symMat[0][1] != tMat[0][1]) = 0
Row Iteration = 2, Row Number = 1
Column Iteration = 1, Column Number = 0, and Row Number = 1
Result of (symMat[1][0] != tMat[1][0]) = 0
Column Iteration = 2, Column Number = 1, and Row Number = 1
Result of (symMat[1][1] != tMat[1][1]) = 0
The Matrix you have entered is a Symmetric