C018 A C program to compute transpose of a matrix

By | September 4, 2013

Let’s identify variables needed for this program.

Transpose of A matrix which is formed by turning all the rows of a given matrix into columns and vice-verse. The transpose of matrix A is written AT.


In this program, we need to save matrices which consists Rows and Columns. To save this we need Two dimensional ARRAY.

Multi dimensional Array is a nothing different than any Array but the only difference is that it has more than one deminsion to it e.g. square has two dimension and cube has three dimension. The size of array is decided by us in number of square brackets [] depending upon the Dimension selected. If We select two dimension than we have to take two square brackets[][]. It has to be accessed with the help of index number ranging from 0 to n-1 and 0 to m-1. (e.g. num[0][0],num[0][1],num[2][2] so on.) 

First few variables will be the one which will save the values entered by the user and it will be array A[4][4] . Here we are taking3x4 matrices hence taking number 3 and 4 in both the square brackets respectively. Transpose of the given matrix will be saved in B[3][3] and displayed later. other variables will be I and J which will be for FOR Loop so in all four variables.
The identified variables are A[3][3],B[3][3],I,J.

int A[3][4],B[4][3],I,J;

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

In this program there is a requirement of accessing the two dimension array A[0][0],A[..][..],..A[3][4]. Here we have the index values ranging from zero to two (i.e. n-1) for ROWS and index values ranging from zero to three (i.e. n-1) for COLUMNS . Best Loop for such kind of condition is FOR Loop the value of loop will start with 0 to 2 and 0 to 3 . Now here we need nested loop one loop inside the other. So that we can access all the twelve elements with it. We will need four nested loop in all eight FOR loops, First two nested loop for scanning and printing required array values of a matrix from the user. Next nested loops for Transposing the values into another array B[4][3]. Next nested loops for printing resultant (Transpose) array values as output.

This let’s you understand nested loop


Transposing the matrix is simple just we have to do is start the column index first instead of row index and row index second instead of column index. Now, save the copy the values of given matrix in reverse index to B[][] and forward index to A[][] (i.e. B[J][I]=A[I][J];).


Finally print the Numbers of Two Dimension Resultant Array in the Matrix form by using Tab and New Line character in the nested Loop at the end of the program.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int A[3][4],B[4][3],I,J;
  5.     clrscr();
  6.     printf("\nENTER MATRIX A VALUES\n");
  7.     for(I=0;I<3;I++)
  8.     {
  9.         for(J=0;J<4;J++)
  10.         {
  11.             scanf("%d",&A[I][J]);
  12.         }
  13.     }
  14.     printf("\nVALUES GIVEN OF MATRIX ARE :\n");
  15.     for(I=0;I<3;I++)
  16.     {
  17.         for(J=0;J<4;J++)
  18.         {
  19.              printf("%d\t",A[I][J]);
  20.         }
  21.         printf("\n");
  22.     }
  23.     for(J=0;J<4;J++)
  24.     {
  25.         for(I=0;I<3;I++)
  26.         {
  27.             B[J][I]=A[I][J];
  28.         }
  29.     }
  30.     printf("\nVALUES GIVEN OF MATRIX ARE :\n");
  31.     for(I=0;I<4;I++)
  32.     {
  33.         for(J=0;J<3;J++)
  34.         {
  35.              printf("%d\t",B[I][J]);
  36.         }
  37.         printf("\n");
  38.     }
  39.     getch();
  40. }

Note:- whenever you have large number of inputs to be entered, we can use space instead of enter button all the time. You can see have its done in Output screen.





Note:- To understand program for sequence in detail Please SEARCH numerically example: C001, C002, etc.

Leave a Reply