# spiral_matrix_v1.c

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