Canna

Tuesday, 25 September 2018

SYMMETRIC MATRIX

  • Problem Description

    A symmetric matrix is a square matrix that is equal to its transpose.

    Write a C program to find whether a given matrix is a square matrix or not.
    Input Format:

    The input consists of (m*n+2) integers. The first integer corresponds to m, the number of rows in the matrix and the second integer corresponds to n, the number of columns in the matrix. The remaining integers correspond to the elements in the matrix. The elements are read in rowwise order, first row first, then second row and so on. Assume that the maximum value of m and n is 10
  • Test Case 1

    Input (stdin)
    3
    
    3
    
    1 2 3
    
    4 5 6
    
    7 8 9
    
    
    Expected Output
    Not Symmetric
  • Test Case 2

    Input (stdin)
    3
    
    3
    
    3 1 1
    
    1 3 1
    
    1 1 5
    
    
    Expected Output
    Symmetric
  • Program
  • #include <stdio.h>
    int main()
    {
     int m, n, c, d, matrix[10][10], transpose[10][10];
     
    
      scanf("%d%d", &m, &n);
     
      for (c = 0; c < m; c++)
        for (d = 0; d < n; d++)
          scanf("%d", &matrix[c][d]);
     
      for (c = 0; c < m; c++)
        for (d = 0; d < n; d++)
          transpose[d][c] = matrix[c][d];
     
      if (m == n) /* check if order is same */
      {
        for (c = 0; c < m; c++)
        {
          for (d = 0; d < m; d++)
          {
            if (matrix[c][d] != transpose[c][d])
              break;
          }
          if (d != m)
            break;
        }
        if (c == m)
          printf("Symmetric\n");
        else
          printf("Not Symmetric\n");
      }
      else
        printf("Not Symmetric\n");
     
     return 0;
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.