please explain algorithm and code in short
Wave print 2d matrix
hello @anish_khare
The algorithm for this problem is simple
a) first u need to read the dimension of the matrix say (M XN)
b) after that u need to read the matrix
c) and then for every even column(column number starts from 0 ) print top to bottom and for every odd column print bottom to top.
#include<iostream>
using namespace std;
int main() {
int M,N;
cin>>M>>N; //reading dimesnions
int arr[100][100];
for(int i=0;i<M;i++){ //reading matrix
for(int j=0;j<N;j++){
cin>>arr[i][j];
}
}
for(int i=0;i<N;i++){ // itearting columwise
if(i%2==0){ // for even column
for(int j=0;j<M;j++){ //printing top to bottom
cout<<arr[j][i]<<", ";
}
}
else{ //for odd column
for(int j=M-1;j>=0;j--){ //printing bottom to top
cout<<arr[j][i]<<", ";
}
}
}
cout<<"END";
return 0;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.