C program to find Transpose of a Matrix

Before going to the program first let us understand what is Transpose of a Matrix?

Transpose of a matrix:

                          Transpose of a Matrix is a matrix that is obtained by interchanging the rows and columns of a matrix.

Transpose of a Matrix can also be obtained by reflecting the elements along its main diagonal. Repeating the process on the transposed matrix returns the elements to their original position.

transpose

For Example,

Let the matrix be
1 2 3
4 5 6
7 8 9

Then the Transpose of the above matrix is
1 4 7
2 5 8
3 6 9

Program Code to Transpose of a matrix in C:

#include <stdio.h>
#include<conio.h>
void main()
{
    int a[10][10],b[10][10],i,j,r,c,tr,tc;
    clrscr();
    printf("\n  Enter the number of rows and columns of matrix : ");
    scanf("%d%d",&r,&c);
    printf("\n  Enter the elements of matrix \n");
    
    for( i = 0 ; i < r ; i++ )
    {
        for( j = 0 ; j < c ; j++ )
        {
            scanf("%d",&a[i][j]);
        }
    }
    tr=c;
    tc=r;
    for( i = 0 ; i < tr ; i++ )
    {
        for( j = 0 ; j < tc ; j++ )
        {
            b[i][j] = a[j][i];
        }
    }
    
    printf("\n  Transpose of matrix is \n\n");
    
    for( i = 0 ; i < tr ; i++ )
    {
        for( j = 0 ; j < tc ; j++ )
        {
            printf("  %d ",b[i][j]);
        }
        printf("\n");
    }
    getch();
}

Working:

  • First the computer reads the rows and columns of matrix from the user.
  • Then using for loops the computer reads the matrix from the user.
  • Then using for loops the transpose of a matrix is carried out.
  • Finally using for loops the transposed matrix is printed.

Step by step working of the above program:

  1. Let us assume that a user enters the number of rows and columns as 3 and 3 respectively.
  2.  And then enters the elements of matrix as given below

1 2 3

4 5 6

7 8 9

  1. tr=c   (tr=3)   ,   tc=r   (tc=3)
  2. It assigns the value of i=0 and j=0 and the loop continues till condition of the for loop is true.

4.1.   i<tr    (0<3)    for loop condition is true

4.1.1.   j<tc     (0<3)    for loop condition is true

b[i][j]=a[j][i]     (b[0][0]=a[o][o])    So  b[0][0]=1

j++     (j=j+1)    So  j=1

4.1.2.   j<tc     (1<3)    for loop condition is true

b[i][j]=a[j][i]     (b[0][1]=a[1][o])    So  b[0][1]=4

j++     (j=j+1)    So  j=2

4.1.3.   j<tc     (2<3)    for loop condition is true

b[i][j]=a[j][i]     (b[0][2]=a[2][o])    So  b[0][2]=7

j++     (j=j+1)    So  j=3

4.1.4.   j<tc     (3<3)    for loop condition is false

It comes out of the for loop and increment the value of i and reassigns the value of j.

i++     (i=i+1)    So   i=1   ,   j=0

4.2.   i<tr    (1<3)    for loop condition is true

4.2.1.   j<tc     (0<3)    for loop condition is true

b[i][j]=a[j][i]     (b[1][0]=a[o][1])    So  b[1][0]=2

j++     (j=j+1)    So  j=1

4.2.2.   j<tc     (1<3)    for loop condition is true

b[i][j]=a[j][i]     (b[1][1]=a[1][1])    So  b[1][1]=5

j++     (j=j+1)    So  j=2

4.2.3.   j<tc     (2<3)    for loop condition is true

b[i][j]=a[j][i]     (b[1][2]=a[2][1])    So  b[1][2]=8

j++     (j=j+1)    So  j=3

4.2.4.   j<tc     (3<3)    for loop condition is false

It comes out of the for loop and increment the value of i and reassigns the value of j.

i++     (i=i+1)    So   i=2   ,   j=0

4.3.   i<tr    (2<3)    for loop condition is true

4.3.1.   j<tc     (0<3)    for loop condition is true

b[i][j]=a[j][i]     (b[2][0]=a[o][2])    So  b[2][0]=3

j++     (j=j+1)    So  j=1

4.3.2.   j<tc     (1<3)    for loop condition is true

b[i][j]=a[j][i]     (b[2][1]=a[1][2])    So  b[2][1]=6

j++     (j=j+1)    So  j=2

4.3.3.   j<tc     (2<3)    for loop condition is true

b[i][j]=a[j][i]     (b[2][2]=a[2][2])    So  b[2][2]=9

j++     (j=j+1)    So  j=3

4.3.4.   j<tc     (3<3)    for loop condition is false

It comes out of the for loop and increment the value of i and reassigns the value of j.

i++     (i=i+1)    So   i=3   ,   j=0

4.4.   i<tr    (3<3)    for loop condition is false

It comes out of the for loop.

  1. Finally using two for loops it prints the elements of the transpose matrix.
  2. Thus the program execution is completed.

Output:

transpose of a matrix

transpose of a matrix

DOWNLOAD THE PROGRAM CODE : CLICK HERE

 

Leave a Reply

Your email address will not be published. Required fields are marked *