Symmetric Matrix In C

Transpose Matrix:

You can find its transpose by interchanging the rows and columns within a matrix. The symbol "T" denotes the transposition of the matrix.

Example:1

If a provided matrix is ```

/*

  • C Program to Determine Whether a Matrix is Symmetric

*/

include<stdio.h>

include<stdlib.h>

int main

{

int i, j, row, col, count = 0;

printf("Please Enter Number of rows and columns: ");

scanf("%d %d", &i, &j);

if(i!=j)

{

printf("Rows not equal to columns. Therefore Non-Symmetric Matrix.");

exit(0);

}

//Set up a 2D array with dimensions i, j.

int ai, bi;

printf("\nEnter the Matrix Elements \n");

for(row = 0; row <i; row++)

{

for(col = 0;col <j;col++)

{

scanf("%d", &arow);

}

}

//Transpose of matrix

for(row = 0; row <i; row++)

{

for(col = 0;col < j; col++)

{

bcol = arow;

}

}

//Verify whether or not matrix A equals matrix B.

for(row = 0; row <i; row++)

{

for(col = 0; col < j; col++)

{

if(arow != brow)

{

count++;

break;

}

}

}

if(count == 0)

{

printf("\nThe given Matrix is a Symmetric Matrix ");

}

else

{

printf("\nThe given Matrix is Not a Symmetric Matrix ");

}

return 0;

}

Example

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.

It is a symmetric matrix because A = A T

Example:2

If a provided matrix is square, then the transpose of the matrix is also square.

It is a symmetric matrix because A ≠ A T

Scenario Description

Let's develop a C program to ascertain if a provided matrix is symmetrical or not.

Resolution

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

Source Code:

Here is the code snippet for a C program that checks if a matrix is symmetrical. The C program compiles and runs without issues on a Linux operating system. The program output is provided below.

Example

/*
 * C Program to Determine Whether a Matrix is Symmetric
 */

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i, j, row, col, count = 0;

printf("Please Enter Number of rows and columns:  ");
scanf("%d %d", &i, &j);

    if(i!=j)
    {
printf("Rows not equal to columns. Therefore Non-Symmetric Matrix.");
exit(0);
    }

    //Set up a 2D array with dimensions i, j.

    int a[i][j], b[i][j];

printf("\nEnter the Matrix Elements \n");
for(row = 0; row <i; row++)
    {
for(col = 0;col <j;col++)
        {
scanf("%d", &a[row][col]);
        }
    }

    //Transpose of matrix
for(row = 0; row <i; row++)
    {
for(col = 0;col < j; col++)
        {
            b[col][row] = a[row][col];
        }
    }

    //Verify whether or not matrix A equals matrix B.
for(row = 0; row <i; row++)
    {
for(col = 0; col < j; col++)
        {
            if(a[row][col] != b[row][col])
            {
                count++;
                break;
            }
        }
    }
if(count == 0)
    {
printf("\nThe given Matrix is a Symmetric Matrix ");
    }
    else
    {
printf("\nThe given Matrix is Not a Symmetric Matrix ");
    }

    return 0;
}

Output:

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

  • 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.
  • Set the integer variable count to 0 .
  • Print it as a non-symmetric matrix and end the program if i is not equal to j .
  • If not, initialise two 2D arrays/matrices, ai and bi , each with a size of i and j.
  • Request that the user insert the components in the matrix a .
  • Use two for loops to calculate the matrix's transpose and store the results in the matrix b . In each iteration of the loop, bcol = arow is used to calculate the transpose of the matrix a do.
  • Determine whether or not matrix A equals matrix B after performing the transposition.
  • To check, run two loops from row = 0 to i and from col = 0 to i , and after each iteration, determine whether or not brow and arow are equal. If it is not equal, raise the count value by 1 and end the loop.
  • 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 ascertain whether the matrix exhibits symmetry, we specify "3" as the number of rows and "3" as the number of columns in this scenario.

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)

Because the for loop iterates n squared times, the aforementioned algorithm to check for matrix symmetry carries a time complexity of O(n2).

Space Complexity O(n2)

Since the n-size 2D arrays are preallocated to store their data, the spatial complexity of a 2D array in the aforementioned program is O(n2) .

Input Required

This code uses input(). Please provide values below: