C++ Program To Find Transpose of a Matrix Last Updated : 21 Jun, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, the transpose of A[][] is obtained by changing A[i][j] to A[j][i]. Example: Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. 1. For Square Matrix The below program finds the transpose of A[][] and stores the result in B[][], we can change N for a different dimension. C++ // C++ Program to find the transpose // of a matrix #include <bits/stdc++.h> using namespace std; #define N 4 // This function stores transpose // of A[][] in B[][] void transpose(int A[][N], int B[][N]) { int i, j; for (i = 0; i < N; i++) for (j = 0; j < N; j++) B[i][j] = A[j][i]; } // Driver code 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], i, j; transpose(A, B); cout << "Result matrix is \n"; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) cout << " " << B[i][j]; cout <<"\n"; } return 0; } OutputResult matrix is 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4The complexity of the above method Time Complexity: O(N*N) as two nested loops are running. Space Complexity: O(N*N) as 2d array is created to store transpose. 2. For Rectangular Matrix The below program finds the transpose of A[][] and stores the result in B[][]. C++ // C++ program to find transpose // of a matrix #include <bits/stdc++.h> using namespace std; #define M 3 #define N 4 // This function stores transpose // of A[][] in B[][] void transpose(int A[][N], int B[][M]) { int i, j; for(i = 0; i < N; i++) for(j = 0; j < M; j++) B[i][j] = A[j][i]; } // Driver code int main() { int A[M][N] = {{1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}}; // Note dimensions of B[][] int B[N][M], i, j; transpose(A, B); cout << "Result matrix is \n"; for(i = 0; i < N; i++) { for(j = 0; j < M; j++) cout << " " << B[i][j]; cout << "\n"; } return 0; } OutputResult matrix is 1 2 3 1 2 3 1 2 3 1 2 3The complexity of the above method Time Complexity: O(N*M) as two nested loops are running. Space Complexity: O(N*M) as 2d array is created to store transpose. 3. In-Place for Square Matrix Below is the implementation of the method: C++ // C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; #define N 4 // Converts A[][] to its transpose 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]); } // Driver code int main() { int A[N][N] = {{1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4}}; transpose(A); printf("Modified matrix is \n"); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) printf("%d ", A[i][j]); printf("\n"); } return 0; } OutputModified matrix is 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 The complexity of the above method Time complexity: O(n) Transpose has a time complexity of O(n + m), where n is the number of columns and m is the number of non-zero elements in the matrix. The computational time for transposing of a matrix using an identity matrix as a reference matrix is O(m*n). Suppose, if the given matrix is a square matrix, the running time will be O(n2). Auxiliary space: O(1). Comment More infoAdvertise with us Next Article C++ Program To Find Transpose of a Matrix K kartik Follow Improve Article Tags : Matrix C++ Programs C++ Computer Science Fundamentals DSA Arrays Arrays C Array Programs +4 More Practice Tags : CPPArraysArraysMatrix Similar Reads C++ Program To Find Normal and Trace of a Matrix Given a 2D matrix, the task is to find Trace and Normal of matrix.Normal of a matrix is defined as square root of sum of squares of matrix elements.Trace of a n x n square matrix is sum of diagonal elements.Examples : Input: mat[][] = {{7, 8, 9}, {6, 1, 2}, {5, 4, 3}}; Output: Normal = 16 Trace = 11 2 min read C++ program to Convert a Matrix to Sparse Matrix Given a matrix with most of its elements as 0, convert this matrix to sparse matrix in C++Examples: Input: Matrix: 0 1 1 1 2 2 2 1 3 3 2 5 4 3 4 Output: Sparse Matrix: 0 1 0 0 0 0 2 0 0 3 0 0 0 0 5 0 0 0 0 4 Explanation: Here the Sparse matrix is represented in the form Row Column Value Hence the ro 3 min read C++ Program For Determinant of a Matrix What is the Determinant of a Matrix? The determinant of a Matrix is a special number that is defined only for square matrices (matrices that have the same number of rows and columns). A determinant is used at many places in calculus and other matrices related to algebra, it actually represents the m 12 min read C++ Program to check if a matrix is symmetric A square matrix is said to be symmetric matrix if the transpose of the matrix is same as the given matrix. Symmetric matrix can be obtain by changing row to column and column to row. Examples:Â Input : 1 2 3 2 1 4 3 4 3 Output : Yes Input : 3 5 8 3 4 7 8 5 3 Output : No A Simple solution is to do fo 3 min read C++ Program to Print matrix in zag-zag fashion Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. Example: Input: 1 2 3 4 5 6 7 8 9 Output: 1 2 4 7 5 3 6 8 9 Approach of C++ code The approach is simple. Just simply iterate over every diagonal elements one at a time and change the directio 3 min read Like