C++ Program to Transpose a Matrix

In this article, you will learn and get code to find transpose of a matrix entered by user using a C++ program. Here are the list of programs available in this article:

  • Find Transpose of a 3*3 Matrix
  • Find Transpose of a Matrix of Given Size

How to Find Transpose of a Matrix ?

Transpose of a matrix can be calculated by interchanging rows and columns. For example, if the value of a matrix say Matrix A is:

1  2  3
4  5  6
7  8  9

Then its transpose (AT) will be:

1  4  7
2  5  8
3  6  9

Find and Print Transpose a Given Matrix in C++

To transpose any matrix in C++ programming, you have to ask from user to enter elements of matrix. Then just replace row with column and column with row to transpose that matrix. Then display or print the transpose of given matrix on output as shown here in the following program:

#include<iostream>
using namespace std;
int main()
{
    int mat[3][3], i, j, matT[3][3];
    cout<<"Enter 9 Elements for 3*3 Matrix: ";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cin>>mat[i][j];
    }
    cout<<"\nOriginal Matrix is:\n";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cout<<mat[i][j]<<"  ";
        cout<<endl;
    }
    // copying the transpose of given matrix to matT[][]
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            matT[j][i] = mat[i][j];
    }
    cout<<"\nTranspose of Given Matrix is:\n";
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            cout<<matT[i][j]<<"  ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program transpose matrix

Now supply the input as 9 elements. Here is the sample output after supplying 9 elements:

transpose a matrix c++

The following block of code:

for(i=0; i<3; i++)
{
    for(j=0; j<3; j++)
        matT[j][i] = mat[i][j];
}

copies the transpose of given matrix mat[][] to matT[][]. Therefore the dry run of this block of code with user input 1, 2, 3, 4, 5, 6, 7, 8, 9 goes like:

  • Because user enters these 9 elements as inputs, therefore it gets stored in mat[][] in this way:
    • mat[0][0]=1
    • mat[0][1]=2
    • mat[0][2]=3
    • mat[1][0]=4
    • mat[1][1]=5
    • and so on upto
    • mat[2][2]=9
  • Now let's come to the block of code, that copies transpose of matrix to matT[][]. Initially 0 gets initialized to i and the condition i<3 or 0<3 evaluates to be true, therefore program flow goes inside the loop
  • Inside the loop, there is another for loop. So 0 gets initialized to j and the condition j<3 or 0<3 evaluates to be true, therefore program flow goes inside this loop and mat[i][j] or mat[0][0] or 1 gets initialized to matT[j][i] or matT[0][0]. So matT[0][0]=1
  • Now program flow goes to the update part of inner for loop and increments the value of j. So j=1 and the condition j<3 or 1<3 evaluates to be true again
  • Therefore program flow goes inside the loop again and mat[i][j] or mat[0][1] or 2 gets initialized to matT[j][i] or matT[1][0]. So mat[1][0]=2
  • Now program flow again goes to the update of inner for loop and increments the value of j. So j=2 and the condition j<3 evaluates to be true again, therefore program flow goes inside the loop again. This process continues until the condition evaluates to be false
  • When the condition of inner for loop evaluates to be false, then program flow goes to the updatation part of outer for loop and increments the value of i. So i=1 and the condition i<3 or 1<3 evaluates to be true again, therefore program flow goes inside the loop. This process continues until the condition evaluates to be false
  • In this way, the transpose of matrix mat[][] gets copied to matT[][].
  • So just print the value of matT[][]

Allow User to Define Matrix Dimension

This program is similar to previous program. The only difference is, here we've allowed user to enter the dimension of matrix along with its elements, before finding and printing the transpose of given matrix:

#include<iostream>
using namespace std;
int main()
{
    int mat[10][10], matT[10][10];
    int matRow, matCol, i, j;
    cout<<"Enter Row and Column Size of Matrix: ";
    cin>>matRow>>matCol;
    cout<<"Enter "<<matRow*matCol<<" Elements for "<<matRow<<"*"<<matCol<<" Matrix: ";
    for(i=0; i<matRow; i++)
    {
        for(j=0; j<matCol; j++)
            cin>>mat[i][j];
    }
    cout<<"\nOriginal Matrix is:\n";
    for(i=0; i<matRow; i++)
    {
        for(j=0; j<matCol; j++)
            cout<<mat[i][j]<<"  ";
        cout<<endl;
    }
    // copying the transpose of given matrix to matT[][]
    for(i=0; i<matRow; i++)
    {
        for(j=0; j<matCol; j++)
            matT[j][i] = mat[i][j];
    }
    cout<<"\nTranspose of Given Matrix is:\n";
    for(i=0; i<matCol; i++)
    {
        for(j=0; j<matRow; j++)
            cout<<matT[i][j]<<"  ";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}

Here is its sample run with user input 2 and 4 as dimension. That is, 2 as row size and 4 as column size, then 1, 2, 3, 4, 5, 6, 7, 8 as its 8 elements. Here is the sample run with these inputs:

c++ transpose a matrix

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »