C++ Program to Transpose Matrix with Output and Explanation
transposed[j][i] = original[i][j]; to create the transpose.Examples
How to Think About It
Algorithm
Code
#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; }
Dry Run
Let's trace the example input 2 3 with matrix [[1,2,3],[4,5,6]] through the code.
Input matrix
rows=2, cols=3, matrix=[[1,2,3],[4,5,6]]
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
Print transpose
Print rows=3, cols=2 of transpose matrix: 1 4 2 5 3 6
| i | j | matrix[i][j] | transpose[j][i] |
|---|---|---|---|
| 0 | 0 | 1 | transpose[0][0] = 1 |
| 0 | 1 | 2 | transpose[1][0] = 2 |
| 0 | 2 | 3 | transpose[2][0] = 3 |
| 1 | 0 | 4 | transpose[0][1] = 4 |
| 1 | 1 | 5 | transpose[1][1] = 5 |
| 1 | 2 | 6 | transpose[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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Using extra matrix | O(rows*cols) | O(rows*cols) | Any matrix size |
| In-place transpose | O(n^2) | O(1) | Square matrices only |
| Using vectors | O(rows*cols) | O(rows*cols) | Dynamic size and safer memory |