Program code: #include
#define down 0 # Define Left 1 # Define Up 2 # Define Right 3
// n * n matrix #define n 5
Void PrintMatrix (int * a [], int N) {INT I, J;
For (i = 0; i Void Spiralmatrix (int * a [], int n) {INT i, J; // coordinate int count; // counter INT K; // cycle variable INT DIRECT; / / direction indication i = 0; j = - N; count = 0; Direct = Down; while (n> 0) {for (k = 0; k Void main (void) {int m [n] [n] = {0}; int * a [n]; int i; For (i = 0; i Scanf ("% d", & c); // input edge length IF (c> 9) Return; / / to remain neat, edge length cannot be greater than 9 A = C-1; B = 0; // Rotary point coordinate I = 1; j = 0; // coordinate increment i = 0; j = 0; // coordinate for (k = 1; k <= c * c ; k ) {s [i] [j] = k; if (i == a && j == b) // encountered turning point {t = a; a = CB-1; b = t (i j> 0? 0: 1); // change the turning point T = i; i = -j; j = t; // change the coordinate increment} i = i; j = j; // change the coordinate} For (j = 0; j Return;}