-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintSpiralMatrix.c
69 lines (59 loc) · 1.04 KB
/
PrintSpiralMatrix.c
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#define DEBUG 1
#define M 5
#define N 5
int mat[M][N] =
{
{ 1, 2, 3, 4, 5},
{16, 17, 18, 19, 6},
{15, 24, 25, 20, 7},
{14, 23, 22, 21, 8},
{13, 12, 11, 10, 9}
};
void printElement(int y, int x)
{
#if DEBUG
printf("Printing location mat[%d][%d]\n", y, x);
#endif
printf("%d ",mat[y][x]);
#if DEBUG
printf("\n");
#endif
}
void printSpiralOrder()
{
int top = 0, bottom = M - 1;
int left = 0, right = N - 1;
while (1)
{
if (left > right)
break;
// print top row
for (int i = left; i <= right; i++)
printf("%d ",mat[top][i]);
top++;
if (top > bottom)
break;
// print right column
for (int i = top; i <= bottom; i++)
printf("%d ",mat[i][right]);
right--;
if (left > right)
break;
// print bottom row
for (int i = right; i >= left; i--)
printf("%d ",mat[bottom][i]);
bottom--;
if (top > bottom)
break;
// print left column
for (int i = bottom; i >= top; i--)
printf("%d ",mat[i][left]);
left++;
}
}
int main()
{
printSpiralOrder();
return 0;
}