C++ Program to Transpose a Matrix

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

How to Find the Transpose of a Matrix

The transposition 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

In C++, find the transpose of a 3*3 matrix

To transpose any matrix in C++ programming, you have to ask the user to enter elements of the matrix. To transpose that matrix, simply replace row with column and column with row. Then display or print the transposition of the given matrix on the 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 built and runs under the Code::Blocks IDE. Here is its sample run:

C++ program transpose matrix

Now supply the input as nine 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 the given matrix mat[][] to matT[][]. Therefore the dry run of this block of code with the user inputs 1, 2, 3, 4, 5, 6, 7, 8, and 9 goes like this:

Allow the user to specify the dimensions of the matrix

This program is similar to the previous program. The only difference is that here we've allowed the user to enter the dimension of the matrix along with its elements before finding and printing the transposition of the 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 inputs 2 and 4 as dimensions. 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

The same program in different languages

C++ Quiz


« Previous Program Next Program »


Liked this post? Share it!