-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23_tranposeMatrix.cpp
70 lines (62 loc) · 1.36 KB
/
23_tranposeMatrix.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
void PrintMatrix(vector<vector<int>> matrix, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
// Function to find transpose of a matrix.
void transpose(vector<vector<int>> &matrix, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
swap(matrix[i][j], matrix[j][i]);
}
}
}
};
//{ Driver Code Starts.
int main()
{
int n;
cout << "enter the row:";
cin >> n;
vector<vector<int>> matrix(n);
cout << "enter the matrix element:" << endl;
for (int i = 0; i < n; i++)
{
matrix[i].assign(n, 0);
for (int j = 0; j < n; j++)
{
cin >> matrix[i][j];
}
}
Solution ob;
cout << "orignal martix:" << endl;
ob.PrintMatrix(matrix, n, n);
cout << "tranpose matrix:" << endl;
ob.transpose(matrix, n);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
// } Driver Code Ends