-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path064TransposeOfMatrix.cpp
48 lines (39 loc) · 1.23 KB
/
064TransposeOfMatrix.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
//program to find transpose of a matrix
// inlcuding required header files
#include <iostream>
using namespace std;
// main function
int main() {
// input rows, columns, elements of the matrix from the user
int rows, cols;
cout << "Enter the number of rows in the matrix: ";
cin >> rows;
cout << "Enter the number of columns in the matrix: ";
cin >> cols;
int matrix[rows][cols], transpose[cols][rows];
cout << "Enter the elements of the matrix " << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << "Enter " << i+1 << "th row & " << j+1 << "th column element: ";
cin >> matrix[i][j];
}
}
// calculating and diplaying the transponse of the matrix
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
transpose[j][i] = matrix[i][j];
}
}
cout << "Transpose of the matrix:" << endl;
for (int i = 0; i < cols; ++i) {
cout << "| ";
for (int j = 0; j < rows; ++j) {
if (j < rows - 1) {
cout << transpose[i][j] << ", ";
} else {
cout << transpose[i][j] << " |" << endl;
}
}
}
return 0;
}