Interview Questions

Spiral Matrix psuedo code(& logic)

Microsoft Interview Questions and Answers


(Continued from previous question...)

84. Spiral Matrix psuedo code(& logic)

Question:
Spiral Matrix psuedo code(& logic).


maybe an answer:


#include <stdio.h>

#define ROW 5
#define COL 5

void printSpiral (int matrix[ROW][COL])
{
int rightCol=COL-1,downRow=ROW-1;
int leftCol=0,upRow=0,index=0;
int count=ROW*COL;
while(count)
{
while((index<=rightCol)&&(count))
{
printf("[%d]", matrix[upRow][index]);
index++;
count--;
}
upRow++;
index=upRow;
while((index<=downRow)&&(count))
{
printf("[%d]", matrix[index][rightCol]);
index++;
count--;
}
rightCol--;
index=rightCol;
while((index>=leftCol)&&(count))
{
printf("[%d]", matrix[downRow][index]);
index--;
count--;
}
downRow--;
index=downRow;
while((index>=upRow)&&(count))
{
printf("[%d]", matrix[index][leftCol]);
index--;
count--;
}
leftCol++;
index=leftCol;
}
}
int main ()
{
int matrix [ROW][COL] = {
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}
};
printSpiral(matrix);
printf("\n");
}

(Continued on next question...)

Other Interview Questions