This blog is under construction

Saturday 27 July 2013

C program to check whether a matrix is symmetric or not

Write a C program to check whether a matrix is symmetric or not.


  #include <stdio.h>
  #define ROW 10
  #define COL 10

  int main() {
        int matrix[ROW][COL], transpose[ROW][COL];
        int i, j, k, n, flag = 0;

        /* get the order of the matrix from the user */
        printf("Enter the order of the matrix:");
        scanf("%d", &n);

        /* get the entries for the input matrix */
        printf("Enter your matrix inputs:\n");
        for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                        scanf("%d", &matrix[i][j]);
                }
        }

        /* find the tranpose of the given matrix */
        for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                        transpose[i][j] = matrix[j][i];
                }
        }

        /* checking whether the input matrix and its transpose are same */
        for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                        if (matrix[i][j] != transpose[i][j]) {
                                flag = 1;
                                goto end;
                        }
                }
        }

  end:
        /* print the output */
        if (flag) {
                printf("Not a Symmetric matrix!!\n");
        } else {
                printf("Symmetric Matrix!!\n");
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the order of the matrix:3
  Enter your matrix inputs:
  1  7  3
  7  4  5
  3  5  9
  Symmetric Matrix!!


No comments:

Post a Comment