Javatpoint Logo
Javatpoint Logo

Symmetric Matrix in C

Introduction:

A square matrix is considered to be symmetric if its transpose equals the supplied matrix. The user can generate a symmetric matrix by switching from row to column and from column to row. Any given matrix A can have its transpose matrix specified as AT. As a result, the condition that A = AT is satisfied by a symmetric matrix A.

Transpose Matrix:

You may determine its transpose by switching rows and columns in a matrix. The letter "T" is used to represent the matrix's transposition.

Example:1

If a given matrix is Symmetric Matrix in C & the transpose of the matrix is Symmetric Matrix in C

It is a symmetric matrix because A = AT

Example:2

If a given matrix is Symmetric Matrix in C & the transpose of the matrix is Symmetric Matrix in C

It is a symmetric matrix because A ≠ AT

Scenario Description

Let's create a C program to determine whether a given matrix is symmetric or not.

Resolution

  1. Indicate how many rows and columns there are in a matrix.
  2. Print a non-symmetric matrix if the number of rows and columns is not equal.
  3. if not, enter the items in matrix A.
  4. Locate the matrix's transposition and store the result in another array B.
  5. Verify that A and its transposition B are equivalent.
  6. If A = B, anything is symmetric; otherwise, it is not.

Source Code:

Here is the source code for the C program that determines whether a matrix is symmetric or not. On a Linux system, the C program is successfully compiled and executed. Also presented below is the output of the program.

Output:

Please Enter Number of rows and columns:  3 3
Enter the Matrix Elements
1 4 5
4 3 2
5 2 1
The provided matrix is symmetric.

Program Description

  1. Request the user's input on the number of columns and rows in a matrix and store it in the variables i and j, accordingly.
  2. Set the integer variable count to 0.
  3. Print it as a non-symmetric matrix and end the program if i is not equal to j.
  4. If not, initialise two 2D arrays/matrices, a[i][j] and b[i][j], each with a size of i and j.
  5. Request that the user insert the components in the matrix a.
  6. Use two for loops to calculate the matrix's transpose and store the results in the matrix b. In each iteration of the loop, b[col][row] = a[row][col] is used to calculate the transpose of the matrix a do.
  7. Determine whether or not matrix A equals matrix B after performing the transposition.
  8. To check, run two loops from row = 0 to i and from col = 0 to i, and after each iteration, determine whether or not b[row][col] and a[row][col] are equal. If it is not equal, raise the count value by 1 and end the loop.
  9. Verify whether count value equals 0 or not. Print the symmetric matrix if it equals 0. Otherwise, print the non-symmetric matrix.

Case Study:

To determine if the matrix is symmetric or not, we input "3" for the number of rows and "3" for the number of columns in this instance.

Please Enter Number of rows and columns: 3 3

Enter the Matrix Elements

1 4 5

4 3 2

5 2 1

The provided matrix is symmetric.

Time Complexity O(n2)

Due to the for loop running n2 times, the above program for determining if a matrix is symmetric or not has an O(n2) time complexity.

Space Complexity O(n2)

Because n-size 2D arrays have been initialized to hold their values, the space complexity of a 2D array in the program above is O(n2).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA