Define the Order N of the spiral matrix, store the spiral matrix in a two-dimensional array of n × n, and print it out.Note: Please respect the individual's labor results, and indicate the source for reprinting.
The implementation code is as follows:
/*************************************** * ********************************** File Name: helixmatrix. c ** created on: 2012-8-15 ** Author: Martin (p000034) ** contact information: Ma.Martin153@gmail.com ** Description: Order n of a custom spiral matrix, store the spiral square matrix in the ** n × n two-dimensional array and print it out. ** Modification: No *** statement: this procedure is only for study and communication and cannot be used for commercial purposes without the author's authorization. ** otherwise, the legal liability is reserved. **************************************** * *********************************/# Include <stdio. h> // The Order of the custom spiral matrix # define n 5int main (void) {int index; // The Int maxindex of the temporary count unit; // The upper bound of the temporary count unit int temprow; // The Int templine of the temporary count unit int maxtemp; // The Upper Bound int tempshowline of the temporary count unit int tempshowline; // display the temporary row counting unit int tempshowrow of the spiral matrix; // The temporary column counting unit int number = 0 used to display the spiral matrix; // accumulators int matrix [N] [N] = {0}; // stores the two-dimensional array of the spiral matrix maxindex = (n + 1)/2-1 ); // cyclically, assign a value to the array for (Index = 0; index <= maxindex; index ++) {temprow = index; templine = index; maxtemp = N-(Index * 2 + 1) + index; // left vertical loop for (; temprow <maxtemp; temprow ++) {matrix [temprow] [templine] = (++ number);} // lower horizontal loop for (; templine <maxtemp; templine ++) {matrix [temprow] [templine] = (++ number);} // right vertical loop for (; temprow> index; temprow --) {matrix [temprow] [templine] = (++ number);} // The upper horizontal loop for (; templine> index; templine --) {matrix [temprow] [templine] = (++ number) ;}// if n is an odd number if (1 = n % 2) {matrix [temprow] [templine] = (++ number) ;}else {;}// display the spiral matrix printf ("% d level spiral matrix: \ n ", n); For (tempshowrow = 0; tempshowrow <n; tempshowrow ++) {for (tempshowline = 0; tempshowline <n; tempshowline ++) {printf ("%-6D", matrix [tempshowrow] [tempshowline]);} printf ("\ n");} return 0 ;}
When the order of the spiral matrix is 5, It is shown as follows: