/* 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 <stdio. h> # include <string. h> # include <stdlib. h> 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 <Col; I ++) A [I] = (int *) malloc (sizeof (INT) * row); contral (A, Col, row); print (A, Col, row); Return 0;} void contral (INT ** Arry, int Col, int row) {int start = 0; while (COL> 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 <Endy) {for (I = start + 1; I <Endy; I ++) {A [I] [endx] = temp; temp ++ ;}} // print the third row from left to right IF (start <endx & start <Endy) {for (I = endx; I> = start; I --) {A [Endy] [I] = temp; temp ++ ;}// print the fourth column from bottom to top if (start <endx & start <endY-1) {for (I = endY-1; I> = start + 1; I --) {A [I] [start] = temp; temp ++ ;}}} void print (INT ** Arry, int Col, int row) {int I, j; for (I = 0; I <Col; I ++) {for (j = 0; j <row; j ++) {printf ("% 4D", arry [I] [J]);} printf ("\ n ");}}
Program running result:
[[email protected] code_test]$ gcc -o print_cir_arry print_cir_arry.c[[email protected] 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
Two-dimensional array ring printing, debugging in person