Skip to content

Commit 82331a0

Browse files
authored
Merge pull request #779 from ishikasinha-d/matrix-transpose
Create transpose_of_matrix.cpp
2 parents 43117f1 + 546c912 commit 82331a0

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

2D Arrays/transpose_of_matrix.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//Write a program to find the transpose of a square matrix of size N*N. The transpose of a matrix is obtained by changing rows to columns and columns to rows.
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
//Function to find transpose of a matrix.
7+
void transpose(vector<vector<int> > &matrix, int n)
8+
{
9+
for(int i=0; i< n; i++)
10+
{
11+
12+
for(int j=i; j< n; j++)
13+
{
14+
swap(matrix[i][j], matrix[j][i]);
15+
}
16+
}
17+
}
18+
19+
int main()
20+
{
21+
22+
int t;
23+
cin>>t;
24+
25+
while(t--)
26+
{
27+
int n;
28+
cin>>n;
29+
vector<vector<int> > matrix(n);
30+
31+
for(int i=0; i<n; i++)
32+
{
33+
matrix[i].assign(n, 0);
34+
for( int j=0; j<n; j++)
35+
{
36+
cin>>matrix[i][j];
37+
}
38+
}
39+
40+
transpose(matrix,n);
41+
for (int i = 0; i < n; ++i)
42+
{
43+
for (int j = 0; j < n; ++j)
44+
cout<<matrix[i][j]<<" ";
45+
cout<<endl;
46+
}
47+
}
48+
return 0;
49+
50+
}

0 commit comments

Comments
 (0)