Pages

Program to get Transpose Matrix in C

We can get the transpose matrix my interchanging row to column and column to row of a matrix.

Source Code:

#include <stdio.h>
#include <conio.h>

void main()
{
   int mat[10][10], trns[10][10],i,j,rw,cl;
   printf("Enter Row and Column of Matrix:");
   scanf("%d %d",&rw, &cl);

   printf("Enter %d * %d Matrix elements:", rw, cl);

   //assigning value to matrix
   for(i = 0; i < rw; i++)
   {
    for(j = 0; j < cl; j++)
      {
      scanf("%d", &mat[i][j]);
      }
   }

   // get transpose of matrix row-col, col - row
    for(i = 0; i < rw; i++)
   {
    for(j = 0; j < cl; j++)
      {
      trns[j][i] = mat[i][j];
      }
   }

   //print original matrix
   printf("\n Original Matrix\n");
   for(i = 0; i < rw; i++)
   {
    for(j = 0; j < cl; j++)
      {
       printf("%d\t", mat[i][j]);
      }
      printf("\n");
   }

  //print transpose matrix
   printf("Transpose Matrix \n");
   for(i = 0; i < cl; i++)
   {
    for(j = 0; j < rw; j++)
      {
      printf("%d\t", trns[i][j]);
      }
      printf("\n");
   }

   getch();
}


Output:
Enter Row and Column of Matrix: 2 2
Enter 2 * 2 Matrix element:1
2
3
4

Original Matrix
1   2
3   4

Transpose Matrix
1    3
2    4

@msucil

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment