Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Examples
3.
Approach 1
4.
Approach 2
5.
Frequently Asked Questions
5.1.
Why is C called a mid-level programming language?
5.2.
What is a built-in function in C?
5.3.
What are header files, and what are their uses in C?
5.4.
What is a matrix in C programming?
5.5.
How do you know if a matrix is an identity or not?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

C Program To Check Symmetric Matrix

Author Komal Shaw
0 upvote

Introduction

If the transpose of a square matrix is the same as the provided matrix, it is said to be symmetric. A symmetric matrix can be created by changing row to column and column to row. The transpose matrix of any given matrix A can be given as AT. A symmetric matrix A, therefore, satisfies the condition, A = AT.

 

Recommended Topic, Binary to Hex Converter and C Static Function.

Examples

Here are some examples of matrices, one of which is a transpose(symmetric) matrix and the other is not.

Input : 1 5 3

        5 1 8

        3 8 3

Output: Yes

 

Input : 3 6 9

        3 4 7

        9 6 3

Output: No

Approach 1

1) Create transpose of the given matrix. 

2) Check if transpose and the given matrices are the same or not.

This is a simple brute force solution, which we will be doing using the for loops.
 

CODE

#include<stdio.h>
int main()
{
  int i, j, rows, columns, a[10][10], b[10][10], c = 1;
  
  printf("\nEnter the number of rows and columns  :  ");
  scanf("%d %d", &i, &j);
 
  printf("\nEnter the matrix elements \n");
  for(rows = 0; rows < i; rows++)
   {
    for(columns = 0;columns < j;columns++)
     {
       scanf("%d", &a[rows][columns]);
     }
   }
    //Finding out the Transpose of matrix 
  for(rows = 0; rows < i; rows++)
   {
    for(columns = 0;columns < j; columns++)
     {
       b[columns][rows] = a[rows][columns];
     }
   }
     
  for(rows = 0; rows < i; rows++)
   {
    for(columns = 0; columns < j; columns++)
     {
     if(a[rows][columns] != b[rows][columns])
     {
     c++;  
break;   
}
      }
   }
   if(c == 1)
   {
   printf("\n The Matrix is a Symmetric Matrix. ");
}
else
{
printf("\n The Matrix is Not a Symmetric Matrix. ");
} 
  return 0;
}

OUTPUT

Enter the number of rows and columns  :  3 3
Enter the matrix elements 
1 5 3
5 1 8
3 8 3
The Matrix is a Symmetric Matrix.

We used For Loop to transpose the given Matrix. Next, we are trying to check whether each element in a matrix is equal to the transposed matrix or not. Thus for each element in the matrix, we are running the inner loop for n times and storing the transpose in another matrix, thereby needing space for that matrix as well.

Time Complexity: O(N x N) 

Auxiliary Space: O(N x N)

You can practice by yourself with the help of online c compiler.

You can also read about dynamic array in c.

Approach 2

Comparing matrix elements without constructing a transpose is an efficient way to check if a matrix is symmetric or not. And for that we need to compare mat[i][j] with mat[j][i]. This is also a simple brute force solution but the space complexity will not be as high as before.
 

CODE

#include <stdio.h>
int main()
{
    int i, j, rows, columns, a[10][10]; int c=0;
     printf("\nEnter the number of rows and columns  :  ");
  scanf("%d %d", &i, &j);
 
  printf("\nEnter the matrix elements \n");
  for(rows = 0; rows < i; rows++)
   {
    for(columns = 0;columns < j;columns++)
     {
       scanf("%d", &a[rows][columns]);
     }
   }
    for (int i = 0; i < rows; i++){
        for (int j = 0; j < columns; j++){
            if (a[i][j] != a[j][i]){
                c=1;
                break;
            }
        }   
    }    
    if(c==1)
    printf("\n The Matrix is Not a Symmetric Matrix. ");
    else
    printf("\n The Matrix is a Symmetric Matrix. ");
}

OUTPUT

Enter the number of rows and columns  :  3 3 
Enter the matrix elements 
1 5 3
5 1 8
3 8 3

The Matrix is a Symmetric Matrix

This is an efficient solution in terms of space as we are not creating any extra matrix to store the transpose of the given matrix. But the time complexity remains the same as we are running the inner loop n time for each element of the matrix.

Time Complexity: O(N x N) 

Auxiliary Space: O(1)

Check out this problem - Matrix Median

Must Read what is storage class in c

Frequently Asked Questions

Why is C called a mid-level programming language?

C is a mid-level programming language because it bridges low-level and high-level computer languages. We may utilize the C language as a System programming language to create the operating system and an Application programming language to create a menu-driven customer-driven billing system. 

What is a built-in function in C?

Built functions, also known as the library functions, are supplied by the system to make a developer's life easier by supporting them in some commonly used predetermined activities. In C, for example, if we need to send the output of our program to the terminal, we would use printf().
 

scanf(), printf(), strcpy, strcmp, strlen, strcat, and many more built-in functions are the most widely used in C.

What are header files, and what are their uses in C?

In C, header files have the extension .h, these header files include function definitions, data type definitions, macros, and so on. The header is used to import the above definitions into the source code, this is done by using #include before the header.  For example, if your source code requires taking input from the user, doing some modification, and printing the output on the terminal, it should include the stdio.h file as #include <stdio.h>, which allows the user to take input using scanf(), and to perform some manipulations, and after that print using printf().

What is a matrix in C programming?

A matrix is a rectangular array of numbers or symbols arranged in rows and columns. 

How do you know if a matrix is an identity or not?

If the given matrix is a square matrix, loop through the array and see if all of the main diagonal elements are 1 and the rest are 0. Set the flag to false and break the loop if any of the conditions are not met. If the flag is set to true, the specified matrix is assumed to be an identity matrix.
 

Conclusion

In this article, we have extensively discussed the Booting in an operating system. We hope this blog has helped you enhance your knowledge regarding Booting in an operating system.

If you want to learn more, check out our articles on Binary to decimal in C, Short int in C Programming Armstrong Number in CRandom function in CC Program to count length of string without using any library functionDifference between c and embedded c , Tribonacci Series.
 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc.

Enrol in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.
 

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass