Two-dimensional array ring printing, debugging in person

Source: Internet
Author: User
Tags print format

/* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.