Transpose of matrix
NOTE: For non square matrix:-
1. First make it square by adding buffer 0's to min(row,col) , |row-col| times.
2. For the swap loop, iterate the rows (i) to max(row,col) times. Keep 0<j<=(i-1)
3. During printing n rows, m columns:-
Input: 0<i<n;0<j<m;
Output:0<i<m;0<j<n;
// Online C++ compiler to run C++ program online
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
// Write C++ code here
int n,m;
n=5;
m=6;
m=max(n,m);
int arr[m][m]={{1,5,7,9,10,11},{6,10,12,13,20,21},{9,25,29,30,32,41},{15,55,59,63,68,70},{40,70,79,81,95,105},{0,0,0,0,0,0}};
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
for(int i=0;i<m;i++){
for(int j=0;j<=i-1;j++){
swap(arr[i][j],arr[j][i]);
}
}
//swap (arr[0][1],arr[1][0]);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Comments
Post a Comment