# spiral_square_matrix_v1.c

Dec 17th, 2019
474
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
2.
3.     spiral_square_matrix_v1.c       by Dragan Milicev
4.
5.
6.     A spiral square matrix of order n x n
7.
8.     starting from the upper left (UL) element M[0][0]
9.
10.     fills in clockwise with numbers from 1 to n*n.
11.
12.
13.  Enter the number (2 <= n <= 19) of the rows (columns)
14.  of the square matrix M[][], n = 4
15.
16.  The matrix M rank is n = 4
17.
18.  Spiral matrix is:
19.
20.    1   2   3   4
21.
22.   12  13  14   5
23.
24.   11  16  15   6
25.
26.   10   9   8   7
27.
28.
29.     You can find all my C programs at Dragan Milicev's pastebin:
30.
31.     https://pastebin.com/u/dmilicev
32.
34.
35. */
36.
37.
38. #include <stdio.h>
39. #include <stdlib.h>
40.
41. #define MAX_SIZE 19         // the highest rank of the matrix M[][]
42.
43.
44. // Displays a matrix M[][] that has r rows and c columns
45. void print_matrix( char *text, int M[][MAX_SIZE], int r, int c )
46. {
47.     int i, j;
48.
49.     printf("\n%s\n\n", text );
50.
51.     for(i=0;i<r;i++) {      // print matrix M[][]
52.         for(j=0;j<c;j++)
53.             printf("%4d", M[i][j] );
54.
55.         printf("\n\n");     // new matrix row
56.     }
57.     printf("\n\n");
58. }
59.
60.
61. // It sets all elements of the matrix M[][] having r rows and c columns to the int number
62. void set_matrix_elements_to_number( int M[][MAX_SIZE], int r, int c, int number ) {
63.     int i, j;
64.
65.     for(i=0;i<r;i++)
66.         for(j=0;j<c;j++)
67.             M[i][j] = number;
68. }
69.
70.
71. // This problem is simplified by filling in a ring-by-ring matrix.
72. // We start from the outer edges and fill all four directions separately
73. // in the order: right, down, left, up.
74. // Then we fill in the next ring ...
75. int main(void)
76. {
77.     int M[MAX_SIZE][MAX_SIZE], n;
78.     int number, r, c, r_min, c_min, r_max, c_max;
79.
80.     printf("\n Enter the number (2 <= n <= %d) of the rows (columns) \n of the square matrix M[][], n = ", MAX_SIZE);
81.     scanf("%d", &n);
82.     printf("\n\n");
83.
84.     if ( n < 2 || n > MAX_SIZE ) {
85.         printf("\n Due to the view, it does not make sense for the matrix row \n to be less than 2 or greater than %d !\n", MAX_SIZE);
86.         exit(1);
87.     }
88.
89.     printf("\n The matrix M rank is n = %d \n\n",n);
90.
91.     // Set all elements of the matrix M[][] to zero to move around the matrix
92.     // by checking the condition: while( M[r][c] == 0 )
93.     set_matrix_elements_to_number(M,n,n,0);
94.
95.     //print_matrix(" The matrix M[][] with zeros is: ", M, n, n);
96.
97.     // initial values
98.     number = 1;     // the first number to be written in the matrix M[][]
99.     r = 0;          // current row
100.     c = 0;          // current column
101.     r_min = 0;      // minimum row          // current ring boundaries
102.     c_min = 0;      // minimum column       // current ring boundaries
103.     r_max = n-1;    // maximum row
104.     c_max = n-1;    // maximum column
105.
106.     while ( number < n*n ){   // number goes from 1 to n*n
107.
108.         // to the right
109.         while( M[r][c] == 0  &&  c <= c_max )
110.             M[r][c++] = number++;
111.
112.         // to the down
113.         r++;    // we increase the current row because we are going down
114.         c--;    // we are reducing the current column since we last enlarged it unnecessarily
115.         while( M[r][c] == 0  &&  r <= r_max )
116.             M[r++][c] = number++;
117.
118.         // to the left
119.         r--;    // we are reducing the current row since we last enlarged it unnecessarily
120.         c--;    // we reduce the current column because we go to the left
121.         while( M[r][c] == 0  &&  c >= c_min )
122.             M[r][c--] = number++;
123.
124.         // to the up
125.         r--;    // we reduce the current row because we go up
126.         c++;    // we are enlarging the current column since we last reduced it unnecessarily
127.         while( M[r][c] == 0  &&  r >= r_min )
128.             M[r--][c] = number++;
129.
130.         r_min++;    // we have filled the outer frame of the matrix M and are now reducing it
131.         c_min++;
132.         r_max--;
133.         c_max--;
134.
135.         r = r_min;  // new starting values for the next inner ring of matrix M
136.         c = c_min;
137.     }
138.
139.     // If n is odd, the above algorithm leaves an unfilled last number
140.     // in the center of the matrix M[n/2][n/2] so let's fill it now:
141.     if ( n % 2 )   // if n is odd
142.         M[n/2][n/2] = n*n;
143.
144.     print_matrix(" Spiral matrix is: ", M, n, n);
145.
146.
147.     return 0;
148. }