0
0
CppProgramBeginner · 2 min read

C++ Program to Transpose Matrix with Output and Explanation

A C++ program to transpose a matrix reads the matrix elements, then swaps rows with columns using nested loops, like transposed[j][i] = original[i][j]; to create the transpose.
📋

Examples

Input2 3 1 2 3 4 5 6
Output1 4 2 5 3 6
Input3 3 1 0 0 0 1 0 0 0 1
Output1 0 0 0 1 0 0 0 1
Input1 4 7 8 9 10
Output7 8 9 10
🧠

How to Think About It

To transpose a matrix, think of flipping it over its diagonal. This means the element at row i and column j in the original matrix moves to row j and column i in the new matrix. You can do this by reading each element and placing it in the swapped position.
📐

Algorithm

1
Get the number of rows and columns from the user.
2
Create a matrix and read its elements.
3
Create another matrix to hold the transpose with swapped dimensions.
4
Use nested loops to assign element at position (i, j) in original to (j, i) in transpose.
5
Print the transposed matrix.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int rows, cols;
    cin >> rows >> cols;
    int matrix[10][10], transpose[10][10];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            cin >> matrix[i][j];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            transpose[j][i] = matrix[i][j];

    for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++)
            cout << transpose[i][j] << " ";
        cout << endl;
    }

    return 0;
}
Output
1 4 2 5 3 6
🔍

Dry Run

Let's trace the example input 2 3 with matrix [[1,2,3],[4,5,6]] through the code.

1

Input matrix

rows=2, cols=3, matrix=[[1,2,3],[4,5,6]]

2

Transpose assignment

transpose[0][0]=1, transpose[1][0]=2, transpose[2][0]=3, transpose[0][1]=4, transpose[1][1]=5, transpose[2][1]=6

3

Print transpose

Print rows=3, cols=2 of transpose matrix: 1 4 2 5 3 6

ijmatrix[i][j]transpose[j][i]
001transpose[0][0] = 1
012transpose[1][0] = 2
023transpose[2][0] = 3
104transpose[0][1] = 4
115transpose[1][1] = 5
126transpose[2][1] = 6
💡

Why This Works

Step 1: Reading matrix

We first read the matrix elements into a 2D array using nested loops.

Step 2: Swapping indices

To transpose, we swap the row and column indices: element at (i, j) moves to (j, i).

Step 3: Printing result

We print the transposed matrix by iterating over the new dimensions.

🔄

Alternative Approaches

In-place transpose for square matrix
cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int matrix[10][10];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> matrix[i][j];

    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            int temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << matrix[i][j] << " ";
        cout << endl;
    }

    return 0;
}
This method works only for square matrices and saves space by swapping elements in place.
Using vector of vectors
cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int rows, cols;
    cin >> rows >> cols;
    vector<vector<int>> matrix(rows, vector<int>(cols));
    vector<vector<int>> transpose(cols, vector<int>(rows));

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            cin >> matrix[i][j];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            transpose[j][i] = matrix[i][j];

    for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++)
            cout << transpose[i][j] << " ";
        cout << endl;
    }

    return 0;
}
Using vectors allows dynamic sizing and safer memory management.

Complexity: O(rows * cols) time, O(rows * cols) space

Time Complexity

The program uses nested loops to visit each element once, so time grows with the number of elements.

Space Complexity

It uses extra space for the transpose matrix equal to the original matrix size.

Which Approach is Fastest?

In-place transpose is fastest and uses less space but only works for square matrices; other methods use extra space but handle all sizes.

ApproachTimeSpaceBest For
Using extra matrixO(rows*cols)O(rows*cols)Any matrix size
In-place transposeO(n^2)O(1)Square matrices only
Using vectorsO(rows*cols)O(rows*cols)Dynamic size and safer memory
💡
Remember that the transpose flips rows and columns, so the output matrix size swaps dimensions.
⚠️
Beginners often forget to swap the dimensions when printing the transposed matrix, causing out-of-bound errors.