Introduction
A square matrix is said to be symmetric if it is equal to its transpose. In other words, for a matrix to be symmetric, the element at position (i, j) must be equal to the element at position (j, i) for all i and j. This guide will show you how to write a C program to check if a given square matrix is symmetric.
Problem Statement
Create a C program that:
- Takes a square matrix as input from the user.
- Checks whether the matrix is symmetric.
- Displays whether the matrix is symmetric or not.
Example:
-
Input:
- Matrix:
1 2 3 2 4 5 3 5 6
- Matrix:
-
Output: The matrix is symmetric.
-
Input:
- Matrix:
1 2 3 4 5 6 7 8 9
- Matrix:
-
Output: The matrix is not symmetric.
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the matrix, its dimensions, and loop counters.
- Input the Dimensions and Elements of the Matrix: Use loops to take input from the user for the matrix.
- Check if the Matrix is Symmetric: Compare each element with its corresponding element in the transpose. If any element does not match, the matrix is not symmetric.
- Display the Result: Use
printfto display whether the matrix is symmetric or not.
C Program to Check if a Matrix is Symmetric
#include <stdio.h>
int main() {
// Step 1: Declare variables to hold the matrix and dimensions
int n;
int matrix[100][100];
int i, j;
int isSymmetric = 1; // Assume the matrix is symmetric initially
// Step 2: Prompt the user to enter the dimension of the square matrix
printf("Enter the dimension of the square matrix (n x n): ");
scanf("%d", &n);
// Step 3: Input the elements of the matrix
printf("Enter elements of the matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Step 4: Check if the matrix is symmetric
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (matrix[i][j] != matrix[j][i]) {
isSymmetric = 0; // Set to 0 if any element does not match
break;
}
}
if (!isSymmetric) {
break;
}
}
// Step 5: Display the result
if (isSymmetric) {
printf("The matrix is symmetric.\n");
} else {
printf("The matrix is not symmetric.\n");
}
return 0; // Step 6: Return 0 to indicate successful execution
}
Explanation
Step 1: Declare Variables
- The variable
nstores the dimension of the square matrix. Thematrixarray stores the elements of the matrix. The variableisSymmetricis a flag initially set to1(true) and is used to check if the matrix is symmetric. The variablesiandjare used as loop counters.
Step 2: Input the Dimension of the Matrix
- The program prompts the user to enter the dimension (
n x n) of the square matrix usingscanf.
Step 3: Input the Elements of the Matrix
- The program uses nested
forloops to take input for each element of the matrix from the user.
Step 4: Check if the Matrix is Symmetric
- The program uses nested
forloops to compare each element with its corresponding element in the transpose:- If
matrix[i][j]is not equal tomatrix[j][i], the matrix is not symmetric, andisSymmetricis set to0(false).
- If
Step 5: Display the Result
- The program checks the value of
isSymmetric:- If it is
1, the matrix is symmetric. - If it is
0, the matrix is not symmetric.
- If it is
Step 6: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example 1:
Enter the dimension of the square matrix (n x n): 3
Enter elements of the matrix:
1 2 3
2 4 5
3 5 6
The matrix is symmetric.
Example 2:
Enter the dimension of the square matrix (n x n): 3
Enter elements of the matrix:
1 2 3
4 5 6
7 8 9
The matrix is not symmetric.
Conclusion
This C program demonstrates how to check if a square matrix is symmetric by comparing its elements with those in the transpose. It covers basic concepts such as arrays, loops, and matrix operations, making it a useful example for beginners learning C programming.