Find Transpose of a Matrix in C++ language using Array
Find transpose of a matrix in c++ language using array. In this article, you will learn how to find the transpose of a matrix in the c++ language using array.
The transpose of a matrix in linear algebra is an operator which flips a matrix over its diagonal.
This switches the rows and columns indices of the matrix A by producing another matrix.
Source Code
// Find Transpose of a Matrix in C++ language using Array
#include <iostream>
using namespace std;
int main() {
int r, c, i, j;
// r - denotes the matrix's rows
// c - dentes the matrix's columns
cout << "-----Enter the number of rows & columns of the matrix-----\n";
cin >> r >> c;
int a[r][c], t[c][r];
// a - denotes the input matrix
// t - denotes the transpose matrix
cout << "\n-----Enter the matrix's elements-----\n";
for (i = 0; i < r; ++i) {
for (j = 0; j < c; ++j) {
cout << "Enter element at position[" << i + 1 << j + 1 << "]: ";
cin >> a[i][j];
}
cout << "\n";
}
// It will display the input matrix
cout << "\n-----The entered matrix-----\n";
for (i = 0; i < r; ++i) {
cout << "\t";
for (j = 0; j < c; ++j) {
cout << a[i][j] << "\t";
if (j == c - 1)
cout << "\n";
}
cout << "\n";
}
// It will transposes matrix
for (i = 0; i < r; ++i) {
for (j = 0; j < c; ++j) {
t[j][i] = a[i][j];
}
}
// It will display the transposed matrix
cout << "\n-----The transpose of the matrix-----\n";
for (i = 0; i < c; ++i) {
cout << "\t";
for (j = 0; j < r; ++j) {
cout << t[i][j] << "\t";
if (j == r - 1)
cout << "\n";
}
cout << "\n";
}
return 0;
}
Output
-----Enter the number of rows & columns of the matrix-----
3
4
-----Enter the matrix's elements-----
Enter element at position[11]: 23
Enter element at position[12]: 43
Enter element at position[13]: 53
Enter element at position[14]: 23
Enter element at position[21]: 3
Enter element at position[22]: 4
Enter element at position[23]: 5
Enter element at position[24]: 6
Enter element at position[31]: 23
Enter element at position[32]: 65
Enter element at position[33]: 76
Enter element at position[34]: 34
-----The entered matrix-----
23 43 53 23
3 4 5 6
23 65 76 34
-----The transpose of the matrix-----
23 3 23
43 4 65
53 5 76
23 6 34