forked from SoumyadeepMukherjee/Matrix_Manipulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiral.c
59 lines (59 loc) · 1.34 KB
/
spiral.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
#include <stdio.h>
#define R 4
#define C 5
void spiralOfMatrix(int enrow, int encol, int arr1[R][C])
{
int i, rowind = 0, colind = 0;
while (rowind < enrow && colind < encol)
{
for (i = colind; i < encol; ++i)
{
printf("%d ", arr1[rowind][i]);
}
rowind++;
for (i = rowind; i < enrow; ++i)
{
printf("%d ", arr1[i][encol-1]);
}
encol--;
if ( rowind < enrow)
{
for (i = encol-1; i >= colind; --i)
{
printf("%d ", arr1[enrow-1][i]);
}
enrow--;
}
if (colind < encol)
{
for (i = enrow-1; i >= rowind; --i)
{
printf("%d ", arr1[i][colind]);
}
colind++;
}
}
}
int main()
{
int i,j;
int arr1[R][C] = { {1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}
};
//------------- print original array ------------------
printf("The given array in matrix form is : \n");
for(i = 0; i < R; i++)
{
for (j=0;j<C;j++)
{
printf("%d ", arr1[i][j]);
}
printf("\n");
}
//------------------------------------------------------
printf("The spiral form of above matrix is: \n");
spiralOfMatrix(R, C, arr1);
return 0;
}