-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransposeOfMatrix.cpp
More file actions
55 lines (46 loc) · 1.11 KB
/
TransposeOfMatrix.cpp
File metadata and controls
55 lines (46 loc) · 1.11 KB
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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
//Function to find transpose of a matrix.
void transpose(vector<vector<int>>& matrix, int n) {
vector<vector<int>> temp(n, vector<int>(n)); // Allocate memory for transposed matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
temp[i][j] = matrix[j][i];
}
}
matrix = temp; // Update mat to point to the transposed matrix
}
};
//{ Driver Code Starts.
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<vector<int> > matrix(n,vector<int>(n));
for(int i=0; i<n; i++)
{
for( int j=0; j<n; j++)
{
cin>>matrix[i][j];
}
}
Solution ob;
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