C++ Program To Find Transpose of a Matrix

Last Updated : 21 Aug, 2026

The transpose of a matrix is obtained by converting its rows into columns and its columns into rows. In other words, for a matrix A, the element at A[i][j] moves to B[j][i] in the transpose.

  • For a matrix of size M × N, its transpose has size N × M.
  • A square matrix can also be transposed in-place without using an extra matrix.

Example:

matrix-transpose

Approaches to Find the Transpose of a Matrix

The transpose of a matrix can be found using the following approaches:

1. Transpose of a Square Matrix Using an Extra Matrix

For a square matrix, create another matrix B of the same size. For every element A[i][j], store it at B[j][i].

  • Create a result matrix B of the same size as A.
  • Traverse every element of A.
  • Store A[i][j] in B[j][i].
  • Print the resulting matrix.
C++
#include <iostream>
using namespace std;

#define N 4

// Stores the transpose of A in B
void transpose(int A[][N], int B[][N])
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            B[j][i] = A[i][j];
        }
    }
}

int main()
{
    int A[N][N] = {
        {1, 1, 1, 1},
        {2, 2, 2, 2},
        {3, 3, 3, 3},
        {4, 4, 4, 4}
    };

    int B[N][N];

    transpose(A, B);

    cout << "Transpose of the matrix:\n";

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << B[i][j] << " ";
        }
        cout << '\n';
    }

    return 0;
}

Output
Transpose of the matrix:
1 2 3 4 
1 2 3 4 
1 2 3 4 
1 2 3 4 

Explanation: The function traverses the original matrix and swaps the row and column indices while storing each element in B[j][i]. Thus, the rows of A become the columns of B.

2. Transpose of a Rectangular Matrix Using an Extra Matrix

For a rectangular matrix of size M × N, the transpose has dimensions N × M. Therefore, the result matrix must have N rows and M columns.

  • Create a result matrix B of size N × M.
  • Traverse the original M × N matrix.
  • Store A[i][j] in B[j][i].
  • Print the resulting N × M matrix.
C++
#include <iostream>
using namespace std;

#define M 3
#define N 4

// Stores the transpose of A in B
void transpose(int A[][N], int B[][M])
{
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            B[j][i] = A[i][j];
        }
    }
}

int main()
{
    int A[M][N] = {
        {1, 1, 1, 1},
        {2, 2, 2, 2},
        {3, 3, 3, 3}
    };

    // Transpose has N rows and M columns
    int B[N][M];

    transpose(A, B);

    cout << "Transpose of the matrix:\n";

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cout << B[i][j] << " ";
        }
        cout << '\n';
    }

    return 0;
} 

Output
Transpose of the matrix:
1 2 3 
1 2 3 
1 2 3 
1 2 3 

Explanation: The original matrix has M rows and N columns, while its transpose has N rows and M columns. The program stores every A[i][j] at B[j][i], which converts rows into columns.

3. In-Place Transpose of a Square Matrix

A square matrix can be transposed without creating another matrix. The idea is to swap elements across the main diagonal. For every pair where j > i, swap A[i][j] with A[j][i]. Elements on the main diagonal remain unchanged.

  • Traverse the upper triangular part of the matrix.
  • For every element A[i][j] where j > i, swap it with A[j][i].
  • The matrix is now transposed in-place.
C++
#include <utility>
using namespace std;

#define N 4

// Transposes the matrix in-place
void transpose(int A[][N])
{
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
            swap(A[i][j], A[j][i]);
        }
    }
}

int main()
{
    int A[N][N] = {
        {1, 1, 1, 1},
        {2, 2, 2, 2},
        {3, 3, 3, 3},
        {4, 4, 4, 4}
    };

    transpose(A);

    cout << "Transpose of the matrix:\n";

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << A[i][j] << " ";
        }
        cout << '\n';
    }

    return 0;
} 
Try It Yourself
redirect icon

Output
Transpose of the matrix:
1 2 3 4 
1 2 3 4 
1 2 3 4 
1 2 3 4 

Explanation: Instead of creating a second matrix, the program directly swaps elements on opposite sides of the main diagonal. Since each pair is swapped only once, the matrix is transposed without using additional storage.

Comment