Transpose of a Matrix in C++ language
Introduction:
“A matrix can be transposed by switching rows to columns as well as columns to rows. To put it another way, A[i][j] becomes A[j][i] when A[N][M] is transposed.”
When the rows and columns from the original matrix are switched, a new matrix known as the transpose matrix is created. To put it another way, inverting the matrix across the diagonal gives the transpose matrix. Following transposition, the dimensions of a matrix with starting dimensions of N×M would change to M×N. The user is prompted to input the total number of rows along with the columns that exist in the program. In this program, the values of the rows and columns must be fewer than 10. The user is next encouraged to fill out the matrix elements. The program determines the matrix's transpose and then shows the result on the screen.
What is Matrix:
A matrix, as the name implies, is a rectangular arrangement of characters, symbols, or numbers arranged according to a specific row along with a column. The components of the matrix are the letters, numbers, and symbols that are contained in it. The order of a matrix is determined by the total number of rows as well as columns it contains. For instance, the matrix [A] “i⨯j” represents a matrix ‘A’ with ‘i’ rows along with ‘j’ columns. In this case, the matrix's order is determined by ‘i⨯j’.
Example:
Original Matrix: The given matrix is the input matrix for which we need to find the transpose is given as follows:
1 2 3
4 5 6
7 8 9
Transpose of the original Matrix: The transpose matrix for the given matrix is as follows:
1 4 6
2 5 8
3 7 8
Finding a matrix's transposition in C++ is a pretty simple process that requires the following steps to be taken:
- Assume that the supplied matrix is a mat of dimension N×M.
- Declaring a 2-dimensional array of dimension M×N, called trans_mat.
- As you iterate from i =0 to N−1, perform the following actions
- Iterate from j=0 to M−1 and execute trans_mati,j = mati,j
- Print the transposed matrix that was obtained.
Pseudocode:
function Transposematrix(mat, N, M):
Transpose_matrice = Matrix of dimensions M * N
for ( i = 0 to N ):
for ( j = 0 to M ):
Transpose_mat[j][i] = mat[i][j]
Return Transpose_matrice
end function
Program to find the transpose of a matrix in C++:
#include <bits/stdc++.h>
using namespace std;
#define N 4
void transposematrix (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];
}
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;
transposematrix(A, B);
cout << "The Resultant transpose matrix is : \n";
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
cout << " " << B[i][j];
cout << "\n";
}
return 0;
}
Output:
![Transpose of a Matrix in C++ language](https://static.tutorialandexample.com/cpp/transpose-of-a-matrix-in-cpp-language1.png)
C++ Program to Determine a Matrix's Transpose Using Constant Space:
You may use constant space to get the transpose of a matrix in C++ by using the following straightforward method: switching components in place for an indeterminate matrix without requiring more memory.
Code Implementation:
#include <iostream>
void t_Matrix (int mat[10][10], int N) {
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
std::swap(mat[i][j], mat[j][i]);
}
}
}
int main() {
int N;
std::cout << "Enter the size of the input matrix : ";
std::cin >> N;
int mat[10][10];
std::cout << "Enter all the elements of the matrix : " << std::endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
std::cin >> mat[i][j];
}
}
t_Matrix (mat, N);
std::cout << "Transpose of the given matrix is : " << std::endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
std::cout << mat[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
Output:
![Transpose of a Matrix in C++ language](https://static.tutorialandexample.com/cpp/transpose-of-a-matrix-in-cpp-language2.png)
- This method is only effective for square matrices, or those in which the total number of rows and columns equals one. Because it solves the problem without requiring additional space, this technique is often referred to as an "in-place" algorithm.
Conclusion:
Hence, to find transpose a given matrix is to change the dimensions of "M*N" to "N*M," which means the transpose matrix is determined by flipping the matrix with respect to the diagonal. By utilizing the above code, one can find the transpose of a given matrix in C++ language.