# Program to check if a matrix is symmetric in C++

C++Server Side ProgrammingProgramming

In linear algebra a matrix M[][] is said to be a symmetric matrix if and only if transpose of the matrix is equal to the matrix itself. A transpose of a matrix is when we flip the matrix over its diagonal, which resultant switches its row and columns indices of the matrix.

Below the example of Symmetric matrix −

$$\begin{bmatrix} 1 & 4 & 7 \\ 4 & 5 & 6 \\ 7 & 6 & 9 \\ \end {bmatrix} \Rightarrow \begin{bmatrix} 1 & 4 & 7 \\ 4 & 5 & 6 \\ 7 & 6 & 9 \\ \end{bmatrix}$$

The above matrix is the symmetric matrix we took the matrix on the left and transpose it and the result is same is equal to the matrix itself.

## Example

Input: arr1[][n] = { { 1, 2, 3 },
{ 2, 2, 4 },
{ 3, 4, 1 } };
Output: its a symmetric matrix
Input: arr1[][n] = { { 1, 7, 3 },
{ 2, 9, 5 },
{ 4, 6, 8 } };
Output: its not a symmetric matrix 

## Approach

We can just follow these steps −

• 1. Take a matrix and store its transpose in other matrix.
• 2. Check the resultant matrix is same as the input matrix.

## Algorithm

Start
Step 1 -> define macro as #define n 10
Step 2 -> declare function to find transporse of a matrix
void transpose(int arr1[][n], int arr2[][n], int a)
Loop For int i = 0 and i < a and i++
Loop For int j = 0 and j < a and j++
Set arr2[i][j] = arr1[j][i]
End
End
Step 3 -> declare function to check symmetric or not
bool check(int arr1[][n], int a)
declare variable as int arr2[a][n]
Call transpose(arr1, arr2, a)
Loop For int i = 0 and i < a and i++
Loop For int j = 0 and j < a and j++
IF (arr1[i][j] != arr2[i][j])
return false
End
End
End
Return true
Step 4 -> In main()
Declare variable as int arr1[][n] = { { 1, 2, 3 },
{ 2, 2, 4 },
{ 3, 4, 1 } }
IF (check(arr1, 3))
Print its a symmetric matrix
Else
Print its not a symmetric matrix
Stop

## Example

#include <iostream>
#define n 10
using namespace std;
//find transporse of a matrix
void transpose(int arr1[][n], int arr2[][n], int a){
for (int i = 0; i < a; i++)
for (int j = 0; j < a; j++)
arr2[i][j] = arr1[j][i];
}
//check symmetric or not
bool check(int arr1[][n], int a){
int arr2[a][n];
transpose(arr1, arr2, a);
for (int i = 0; i < a; i++)
for (int j = 0; j < a; j++)
if (arr1[i][j] != arr2[i][j])
return false;
return true;
}
int main(){
int arr1[][n] = { { 1, 2, 3 },
{ 2, 2, 4 },
{ 3, 4, 1 } };
if (check(arr1, 3))
cout << "its a symmetric matrix";
else
cout << "its not a symmetric matrix";
return 0;
}

## Output

its a symmetric matrix