Introduction

A symmetric matrix is a square matrix that is equal to its transpose. In other words, for a matrix AAA to be symmetric, it must satisfy the condition A[i][j]=A[j][i]A[i][j] = A[j][i]A[i][j]=A[j][i] for all elements. Symmetric matrices are important in various fields, including linear algebra, physics, and computer science, as they often simplify computations and have special properties.

#include <stdio.h>

#define MAX 10  // Define the maximum size of the matrix

void inputMatrix(int matrix[MAX][MAX], int size) {
    printf("Enter elements of the matrix (%d x %d):\n", size, size);
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

int isSymmetric(int matrix[MAX][MAX], int size) {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (matrix[i][j] != matrix[j][i]) {
                return 0;  // Not symmetric
            }
        }
    }
    return 1;  // Symmetric
}

void printMatrix(int matrix[MAX][MAX], int size) {
    printf("Matrix (%d x %d):\n", size, size);
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int matrix[MAX][MAX];
    int size;

    printf("Enter the size of the matrix: ");
    scanf("%d", &size);

    inputMatrix(matrix, size);
    
    printf("Original Matrix:\n");
    printMatrix(matrix, size);
    
    if (isSymmetric(matrix, size)) {
        printf("The matrix is symmetric.\n");
    } else {
        printf("The matrix is not symmetric.\n");
    }

    return 0;
}

Explanation

  1. Includes and Defines: The program includes the stdio.h header for input/output functions and defines a constant MAX for the maximum size of the matrix.
  2. Input Function: The inputMatrix function prompts the user to enter the elements of a square matrix (of size size x size).
  3. Symmetry Check Function: The isSymmetric function checks if the matrix is symmetric. It iterates through the elements, comparing A[i][j]A[i][j]A[i][j] with A[j][i]A[j][i]A[j][i]. If any pair does not match, it returns 0 (not symmetric). If all pairs match, it returns 1 (symmetric).
  4. Print Function: The printMatrix function displays the matrix in a formatted way.
  5. Main Function: In main, the program:
    • Declares a matrix.
    • Asks for the size of the matrix.
    • Calls the input, print, and symmetry check functions to display the results.

Input

The user is prompted to enter the size of the matrix, followed by the matrix elements. Example input:

Enter the size of the matrix: 3
Enter elements of the matrix (3 x 3):
1 2 3
2 4 5
3 5 6

Output

The program will output whether the matrix is symmetric. For the example input, the output will be:

Original Matrix:
1 2 3 
2 4 5 
3 5 6 

The matrix is symmetric.

Conclusion

This C program effectively demonstrates how to check if a matrix is symmetric using a straightforward approach. Understanding symmetric matrices is essential in various applications, including solving systems of equations and optimization problems. The code can be easily adapted for larger matrices or different data types, providing a solid foundation for further exploration of matrix properties in C.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights