See the following code
#define ROWS 5
#define COLS 5
int main()
{
int arr[ROWS][COLS] = {{ 1, 2, 3, 4, 17},
{ 5, 6, 7, 8, 18},
{ 9,10,11,12, 19},
{13,14,15,16, 20},
{21,22,23,24, 25}};
// Four direction counters of current movement
// Horizontal right, vertical bottom, horizontal left and vertical top respectively
int hr, vb, hl, vt;
// levl indicates current depth of our imaginary rectangle into array. Starting value is zero
// since we are looping on the boundaries and ending value is the inner most rectangle
int levl;
for (levl=0; levl < COLS-levl; levl++)
{
for(hr=levl; hr < COLS-levl; hr++) // go right
printf("%d ,", arr[levl][hr]);
for(vb=levl+1; vb < COLS-levl; vb++) // go down
printf("%d ,", arr[vb][hr-1]);
for(hl=vb-1; hl-1 >= levl; hl--) // go left
printf("%d ,", arr[vb-1][hl-1]);
for(vt=vb-1; vt-1 > levl; vt--) // go up
printf("%d ,", arr[vt-1][hl]);
}
return 0;
}
Output
1 ,2 ,3 ,4 ,17 ,18 ,19 ,20 ,25 ,24 ,23 ,22 ,21 ,13 ,9 ,5 ,6 ,7 ,8 ,12 ,16 ,15 ,14 ,10 ,11 ,