How to traverse vertical

how to print 1 4 7 in a 2-D matrix form?
2 5 8
3 6 9

1 Like

for(int i=0;i<col;i++){
for(j=0;j<row;j++) {
cout<<matrix[ j ][ i ]<<" ";
}
cout<<endl;
}

hi @edsrujana1
are you not satisfied with previous solution

have you some other doubts regarding this?
feel free to ask

How to print [1,4,7,],[2,5,8],[3,6,9] in a matrix?

your maxtrix is
1 2 3
4 5 6
7 8 9

and you want to print
[1,4,7,],[2,5,8],[3,6,9]
right??

for that just reverse the loops

for(int i=0;i<col;i++){
 cout<<"["
  for(int j=0;j<row;j++{
     cout<<arr[i][j]<<",";
  }

cout<<"]\n";
}

here you are iterating in each col
for i=0 means 0th column
you are iterating in every row j=0-row

so in this way it just print array col wise(vertically)

i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course

I dont want to change the matrix from [1,2,3],[4,5,6],[7,8,9]. i just want to print [1,4,7],[2,5,8],3,6,9] matrix initially.

i have not change the matrix
i just print it vertically

you can also check by printing the matrix after that

okay sir. got it. thank you

1 Like