Given a 2D array A, your task is to convert all rows to columns and columns to rows.
Input:
First line contains 2 space separated integers, N - total rows, M - total columns.
Each of the next N lines will contain M space separated integers.
Output:
Print M lines each containing N space separated integers.
Constraints:
1≤N≤10
1≤M≤10
0≤A[i][j]≤100 where 1≤i≤N and 1≤j≤M
Solution
Input:
First line contains 2 space separated integers, N - total rows, M - total columns.
Each of the next N lines will contain M space separated integers.
Output:
Print M lines each containing N space separated integers.
Constraints:
1≤N≤10
1≤M≤10
0≤A[i][j]≤100 where 1≤i≤N and 1≤j≤M
Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int row,col; | |
cin>>row>>col; | |
int m[row][col]; | |
int mt[col][row]; | |
for(int i=0;i<row;i++) | |
{ | |
for(int j=0;j<col;j++) | |
{ | |
cin>>m[i][j]; | |
mt[j][i]=m[i][j]; | |
} | |
} | |
for(int k=0;k<col;k++) | |
{ | |
for(int l=0;l<row;l++) | |
{ | |
cout<<mt[k][l]<<" "; | |
} | |
cout<<endl; | |
} | |
return 0; | |
} |
No comments:
Post a Comment