C program of Transpose of Matrix | Matrix Programs in C

Mr Coder February 23, 2013 0

C program of Transpose of Matrix : Transpose of Matrix is one of the most important operations on Matrices and quite easy one too.  Transpose of Matrix means exchanging all rows of Matrix with columns of Matrix and vice versa.  Consider an Example, say i have a matrix A defined below :

A     =

1      2     3

4      5     6

Now Transpose of Matrix A   will be,A=

1     4

2     5

3       6

Now lets see how to write a C program to transpose a matrix. As we know we only need to interchange the rows and columns of Matrices to obtain the transpose, so its quite easy operation. Below i have created a simple C program which will transpose of Given Matrix. It accepts number of Rows and columns and its elements as input and generates output Transpose matrix.

C program of Transpose of Matrix :

#include <stdio.h>
#include <conio.h> 
int main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];

   printf("Enter number of rows and columns of matrix :- ");
   scanf("%d%d",&m,&n);
   printf("\nEnter the elements of [%d x %d] matrix :- \n",m,n);
 /*Get Elements of matrix*/
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         scanf("%d",&matrix[c][d]);
      }
   }
 /*Printing Entered Matrix*/
  printf("\nEntered Matrix is :- \n");
  for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         printf("%d\t",matrix[c][d]);
      }
      printf("\n"); 
   }
 /*Transpose Matrix */  
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         transpose[d][c] = matrix[c][d];
      }
   }
 /* Printing Transpose Matrix*/
   printf("\n\n\nTranspose of entered matrix :-\n");

   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",transpose[c][d]);
      }  
      printf("\n");
   }
   getch();
   return 0;
}

Output of Program :

Enter number of rows and columns of matrix :- 2 3

Enter the elements of [2 x 3] matrix :-
1
2
3
4
5
6

Entered Matrix is :-
1       2       3
4       5       6

Transpose of entered matrix :-
1       4
2       5
3       6

If you have any doubts or queries ask me in form of comments.

Leave A Response »