Two-dimensional array ring printing, debugging in person
/* Print the data to the two-dimensional array according to the print format, and then output the two-dimensional array.
* Time complexity O (N * M)
* Space complexity O (N * M)
*/
# Include
# Include
# Include
Int input (void); void contral (int ** arry, int col, int row); void set_value (int ** a, int col, int row, int start ); void print (int ** arry, int col, int row); int main (void) {int col, row; int ** a; int I; printf ("input the col, row:"); scanf ("% d", & col, & row); // apply for a two-dimensional array a = (int **) malloc (sizeof (int *) * col); for (I = 0; I
Start * 2 & row> start * 2) {set_value (arry, col, row, start); start ++ ;}} void set_value (int ** a, int col, int row, int start) {int endX = row-1-start; int endY = col-1-start; int I; static int temp = 1; // The first line, from right to left for (I = start; I <= endX; I ++) {a [start] [I] = temp; temp ++;} // second column, from top to bottom if (start
= Start; I --) {a [endY] [I] = temp; temp ++ ;}// print the fourth column, from bottom to top if (start
= Start + 1; I --) {a [I] [start] = temp; temp ++ ;}} void print (int ** arry, int col, int row) {int I, j; for (I = 0; I
Program running result:
[trageday@lei-yum code_test]$ gcc -o print_cir_arry print_cir_arry.c[trageday@lei-yum code_test]$ ./print_cir_arry input the col,row:5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9