Comments on: Traversing a matrix in spiral order https://demo.ritambhara.in/traversing-a-matrix-in-spiral-order/ Coding / System Design Interviews Fri, 24 Nov 2017 16:11:18 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: Huy https://demo.ritambhara.in/traversing-a-matrix-in-spiral-order/#comment-1726 Fri, 24 Nov 2017 16:11:18 +0000 http://www.ritambhara.in/?p=343#comment-1726 does anybody have code on FREE PASCAL???

]]>
By: Hem Kumar Newar https://demo.ritambhara.in/traversing-a-matrix-in-spiral-order/#comment-1725 Sat, 22 Mar 2014 18:08:24 +0000 http://www.ritambhara.in/?p=343#comment-1725 but this code works only for square matrix. can you give a more generalized code for any matrix size.

]]>
By: olivia https://demo.ritambhara.in/traversing-a-matrix-in-spiral-order/#comment-1724 Wed, 21 Aug 2013 13:05:41 +0000 http://www.ritambhara.in/?p=343#comment-1724 This will not work for cases where row and column sizes are different and specially in cases where rowstart equals rowend and columnstart equals columnend. in those cases there will be duplicacy of the results printed.
working code :
void traverseMatrixSpirally(int ** a, int numOfRows , int numOfCols).
{
int cs = 0;.
int rs = 0;.
int ce = numOfCols-1;.
int re = numOfRows-1;.
int i, j;
while(rs<= re && cs <= ce).
{
for (i = rs, j = cs ; j<= ce; j++).
Console::Write(" " + a[i][j]);.
if(rs==re)
return;
for(j–, i=rs+1;i=cs; j–).
Console::Write(” ” + a[i][j]);.
for(j++, i=re-1;i>=rs+1;i–)
Console::Write(” ” + a[i][j]);.
cs++; rs++; ce–; re–;.
}
}

]]>
By: Olivia Biswas https://demo.ritambhara.in/traversing-a-matrix-in-spiral-order/#comment-1723 Wed, 21 Aug 2013 13:03:33 +0000 http://www.ritambhara.in/?p=343#comment-1723 This will not work for cases where row and column sizes are different and specially in cases where rowstart equals rowend and columnstart equals columnend. in those cases there will be duplicacy of the results printed.
working code :
void traverseMatrixSpirally(int ** a, int numOfRows , int numOfCols).
{
int cs = 0;.
int rs = 0;.
int ce = numOfCols-1;.
int re = numOfRows-1;.
int i, j;
while(rs<= re && cs <= ce).
{
for (i = rs, j = cs ; j<= ce; j++).
Console::Write(" " + a[i][j]);.
if(rs==re)
return;
for(j–, i=rs+1;i<=re;i++)
Console::Write(" " + a[i][j]);.
if(cs==ce)
return;
for(i–, j=ce-1; j>=cs; j–).
Console::Write(" " + a[i][j]);.
for(j++, i=re-1;i>=rs+1;i–)
Console::Write(" " + a[i][j]);.
cs++; rs++; ce–; re–;.
}
}

]]>
By: Kamal Rawat https://demo.ritambhara.in/traversing-a-matrix-in-spiral-order/#comment-1722 Thu, 20 Sep 2012 08:03:01 +0000 http://www.ritambhara.in/?p=343#comment-1722 In reply to Kuchu.

The main for loop has 4 loops.. the first inner loop will traverse the top row (left to right), second the last column (Top to Down), 3rd will traverse the last row (Right to Left) and final will traverse the first column (Bottom-Up)..
Once the first pass is over (two top/bottom rows and left/right columns are printed).. then the moves on to print the inner matrix.. the start point will be (1,1) hence 2nd row from top will be printed, similarly the 2nd last column will be printed followed by 2nd last row and 2nd column..
And so on..

]]>
By: Kuchu https://demo.ritambhara.in/traversing-a-matrix-in-spiral-order/#comment-1721 Wed, 19 Sep 2012 17:45:17 +0000 http://www.ritambhara.in/?p=343#comment-1721 i think this is doing something different. You very first for loop is going down on the left instead of going right at the top. IOW, I don’t think you should be fixing i and incrementing j.

]]>
By: Kamal Rawat https://demo.ritambhara.in/traversing-a-matrix-in-spiral-order/#comment-1720 Thu, 16 Aug 2012 15:39:02 +0000 http://www.ritambhara.in/?p=343#comment-1720 In reply to chinky4u.

My bad.. messed it up during copy-pasting the code from computer to website.. thanks for pointing it out.

]]>
By: chinky4u https://demo.ritambhara.in/traversing-a-matrix-in-spiral-order/#comment-1719 Thu, 16 Aug 2012 05:45:38 +0000 http://www.ritambhara.in/?p=343#comment-1719 can i please see the code?

]]>