-
Notifications
You must be signed in to change notification settings - Fork 3
/
matrixTranspose.cpp
51 lines (43 loc) · 1.24 KB
/
matrixTranspose.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;
int main() {
int rows, cols;
// Input the number of rows and columns of the matrix
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
// Create the original matrix and the transpose
int matrix[rows][cols];
int transpose[cols][rows];
// Input the elements of the matrix
cout << "Enter the elements of the matrix:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}
// Find the transpose of the matrix
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
transpose[i][j] = matrix[j][i];
}
}
// Display the original matrix
cout << "Original Matrix:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
// Display the transpose matrix
cout << "Transpose of the Matrix:" << endl;
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
cout << transpose[i][j] << " ";
}
cout << endl;
}
return 0;
}