Spiral Matrix

I ran into this challenge described in C/C++ as follows:

#define MATRIX_SIZE 3

#if MATRIX_SIZE == 3

int matrix[3][3] = {

    { 11, 12, 13 },

    { 21, 22, 23 },

    { 31, 32, 33 }

};

#elif MATRIX_SIZE == 4

int matrix[4][4] = {

{ 11, 12, 13, 14 },

{ 21, 22, 23, 24 },

{ 31, 32, 33, 34 },

{ 41, 42, 43, 44 }

};

#else

int matrix[6][6] = {

{ 11, 12, 13, 14, 15, 16 },

{ 21, 22, 23, 24, 25, 26 },

{ 31, 32, 33, 34, 35, 36 },

{ 41, 42, 43, 44, 45, 46 },

{ 51, 52, 53, 54, 55, 56 },

{ 61, 62, 63, 64, 65, 66 }

};

#endif

Given a rectangular matrix of integers [editorial: could be any type of data if one uses generics; I will skip that part] display the values in a spiral fashion. For example, the first matrix[3][3] would produce:  11 12 13 23 33 32 31 11 22. Continue reading “Spiral Matrix”