codescracker
c++

C++ Program to Transpose a Matrix



« Previous Program Next Program »

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:

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:

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 »



© Copyright 2021. All Rights Reserved.

CodesCracker