Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

Dr. A.P.J. Abdul Kalam Technical University, Lucknow

KCS151 / KCS251 Programming for Problem Solving - Using C Language


Lab Exercises


28. Write a program to find the transpose of a given matrix & check whether it is symmetric or not.


/*
 File: Prgrm28.c
 Author: Aditya Saini
 Date: Jan 18, 2021
 Description: Program to find the transpose of a given matrix & check whether
 it is symmetric or not.
*/

#include <stdio.h>

int main (void)
{
     int mat[20][20], t_mat[20][20];
     int row, col;
     int sym;
     int i, j;

     //Input row and column
     printf ("Input no of rows: ");
     scanf ("%d", &row);
     printf ("Input no of columns: ");
     scanf ("%d", &col);

     //Input matrix
     printf ("\n***Input matrix***\n");
     for (i = 0; i <= row - 1; i++)
          for (j = 0; j <= col - 1; j++)
          {
               printf ("Input element %d, %d: ", i + 1, j + 1);
               scanf ("%d", &mat[i][j]);
          }

     //Calculate transpose matrix
     for (i = 0; i <= row - 1; i++)
          for (j = 0; j <= col - 1; j++)
               t_mat[j][i] = mat[i][j];

     //Check if the matrix is symmetric or not
     sym = 1;
     for (i = 0; i <= row - 1; i++)
          for (j = 0; j <= col - 1; j++)
               if (mat[i][j] != t_mat[i][j])
                    sym = 0;

     //Print matrix
     printf ("\n***Matrix***\n");
     for (i = 0; i <= row - 1; i++)
     {
          for (j = 0; j <= col - 1; j++)
               printf ("%d\t", mat[i][j]);
          printf ("\n");
     }

     //Print transpose matrix
     printf ("\n***Transpose Matrix***\n");
     for (i = 0; i <= col - 1; i++)
     {
          for (j = 0; j <= row - 1; j++)
               printf ("%d\t", t_mat[i][j]);
          printf ("\n");
     }

     //Print symmetric or not
     if (sym)
          printf ("\nMatrix is symmetric.");
     else
          printf ("\nMatrix is asymmetric.");

     return 0;
};

Output

Input no of rows: 3
Input no of columns: 3

***Input matrix***
Input element 1, 1: 1
Input element 1, 2: 2
Input element 1, 3: 3
Input element 2, 1: 2
Input element 2, 2: 3
Input element 2, 3: 1
Input element 3, 1: 3
Input element 3, 2: 1
Input element 3, 3: 2

***Matrix***
1      2      3
2      3      1
3      1      2

***Transpose Matrix***
1      2      3
2      3      1
3      1      2

Matrix is symmetric.



No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib