Transpose of a Matrix
Transpose of a Matrix in C++ (4 Programs With Output)
C++ Transpose of a Matrix Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int rows = 3, cols = 3;
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int transpose[3][3];
// Calculate transpose
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
cout << "Original matrix:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << "Transpose matrix:" << endl;
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
cout << transpose[i][j] << " ";
}
cout << endl;
}
return 0;
}Original matrix: 1 2 3 4 5 6 7 8 9 Transpose matrix: 1 4 7 2 5 8 3 6 9
Understanding Transpose of a Matrix
This program teaches you how to transpose a matrix in C++. The transpose of a matrix is obtained by swapping its rows and columns. For example, if a matrix has element at position [i][j], in the transpose it will be at position [j][i]. Matrix transposition is a fundamental operation in linear algebra, used extensively in mathematics, computer graphics, and data science.
---
1. What This Program Does
The program transposes a matrix by swapping rows and columns. For example:
1 2 3
4 5 6
7 8 9
1 4 7
2 5 8
3 6 9
The first row becomes the first column, the second row becomes the second column, and so on.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Matrix Transposition
Definition
:
If A is a matrix, then A^T (transpose) is obtained by:
Mathematical Notation
:
If A[i][j] is an element at row i, column j, then:
A^T[j][i] = A[i][j]
Key Property
:
---
4. Declaring Variables
The program declares:
int rows = 3, cols = 3;
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int transpose[3][3];
---
5. The Transposition Algorithm
The core algorithm uses nested loops:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
How it works
:
Step-by-step
(for 3×3 matrix):
Iteration 1:
i=0, j=0
Iteration 2:
i=0, j=1
Iteration 3:
i=0, j=2
Iteration 4:
i=1, j=0
And so on...
Visual Representation
:
[0][0] [0][1] [0][2] [0][0] [1][0] [2][0]
[1][0] [1][1] [1][2] -> [0][1] [1][1] [2][1]
[2][0] [2][1] [2][2] [0][2] [1][2] [2][2]
---
6. Displaying the Matrices
The program displays both matrices:
Original Matrix:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
}
cout << endl;
}
cout << matrix[i][j] << " ";Transpose Matrix:
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
}
cout << endl;
}
---
cout << transpose[i][j] << " ";7. Other Methods (Mentioned but not shown in code)
Method 2: In-Place Transpose (Square Matrices Only)
for (int i = 0; i < rows; i++) {
for (int j = i + 1; j < cols; j++) {
swap(matrix[i][j], matrix[j][i]);
}
}
Method 3: Using Functions
void transposeMatrix(int matrix[][3], int transpose[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
}
Method 4: Using Vectors
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
vector<vector<int>> result(cols, vector<int>(rows));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[j][i] = matrix[i][j];
}
}
}
---
return result;8. Understanding the Index Swap
Key Insight
:
The transpose operation swaps the indices:
Why This Works
:
---
9. When to Use Each Method
-
Nested Loops with New Matrix
: Best for learning - clear and straightforward.
-
In-Place Transpose
: Best for square matrices when memory is limited.
-
Functions
: Best for code organization - reusable and modular.
-
Vectors
: Best for dynamic matrices - flexible and modern C++.
Best Practice
: Use nested loops for learning, functions for production code, vectors for dynamic sizes.
---
10. Important Considerations
Square vs Rectangular Matrices
:
Memory Requirements
:
Index Bounds
:
---
11. Common Use Cases
Linear Algebra
:
Computer Graphics
:
Data Science
:
---
12. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning matrix operations, understanding 2D arrays, and preparing for linear algebra, computer graphics, and data science applications in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Transpose of a Matrix
This C++ program is part of the "Array Operations Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.