
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Find Transpose of a Matrix
A matrix is a rectangular array of numbers arranged in rows and columns. To find the transpose of a matrix, we arrange the rows of the original matrix as columns in a new matrix. Similarly, the columns can also be arranged as rows.
An example of the transpose of a matrix is as follows:

Finding Transpose of a Matrix
To find the transpose of a matrix in C++, we use multidimensional arrays to store matrix values in rows and columns. Then, we swap the rows and columns to get the transpose. Below are the steps we followed:
- We first define the number of rows and columns of the original matrix.
- Next, we create a 2D array a and initialize it with values to represent the matrix.
- Then, we create another 2D array called transpose to store the result.
- We copy each element from the original matrix to the transpose matrix by switching its row and column positions. In simple terms, the value at a[i][j] is placed at transpose[j][i].
- Finally, we print the transpose matrix to show the result.
C++ Program to Find the Transpose of a Matrix
Here is a complete C++ program where we find the transpose of a matrix by swapping its rows and columns:
#include<iostream> using namespace std; int main() { int transpose[10][10], r=3, c=2, i, j; int a[3][3] = { {1, 2} , {3, 4} , {5, 6} }; // Printing the original matrix cout<<"The matrix is:"<<endl; for(i=0; i<r; ++i) { for(j=0; j<c; ++j) cout<<a[i][j]<<" "; cout<<endl; } cout<<endl; // Storing transpose by flipping rows and columns for(i=0; i<r; ++i) for(j=0; j<c; ++j) { transpose[j][i] = a[i][j]; } // printing the transposed matrix cout<<"The transpose of the matrix is:"<<endl; for(i=0; i<c; ++i) { for(j=0; j<r; ++j) cout<<transpose[i][j]<<" "; cout<<endl; } return 0; }
Below you will see the output of the above program, which shows the transpose of a matrix.
The first matrix is: The matrix is: 1 2 3 4 5 6 The transpose of the matrix is: 1 3 5 2 4 6
Time Complexity: O(r*c) because we have to process each element to create the transpose.
Space Complexity: O(r*c) because we need extra space to store all those transposed elements.