forked from TECHOUS/DSKaKhel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralOrderMat.cpp
More file actions
56 lines (50 loc) · 1023 Bytes
/
SpiralOrderMat.cpp
File metadata and controls
56 lines (50 loc) · 1023 Bytes
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
#include<stdio.h>
#include<malloc.h>
int main()
{
int rows,cols,i,j,k;
scanf("%d",&rows);
scanf("%d",&cols);
int arr[rows][cols] = {0};
for(i = 0 ; i < rows ; i++)
{
for(j = 0 ; j < cols ; j++)
{
scanf("%d",&arr[i][j]);
}
}
int startingRow = 0;
int startingCol = 0;
int endingRow = rows-1;
int endingCol = cols-1;
while(startingRow <= endingRow && startingCol <= endingCol)
{
for(j = startingCol ; j <= endingCol ; j++)
{
printf("%d\n",arr[startingRow][j]);
}
startingRow++;
for(j = startingRow ; j <= endingRow ; j++)
{
printf("%d\n",arr[j][endingCol]);
}
endingCol--;
if(startingCol < endingRow)
{
for(j = endingCol ; j>=startingCol ; --j)
{
printf("%d\n",arr[endingRow][j]);
}
endingRow--;
}
if(startingCol<endingCol)
{
for(j = endingRow ; j>=startingRow ; --j)
{
printf("%d\n",arr[j][startingCol]);
}
startingCol++;
}
}
return 0 ;
}