Enter a dimension and print out a specified matrix counterclockwise.

Source: Internet
Author: User

Question: If a user gives a dimension and prints a specified matrix, for example, if the user gives 10, the output should be shown in:

The procedure is as follows:

# Include <stdio. h> # include <malloc. h> int main () {int dimension; int * P; int startx, starty, endx, Endy; int I, j, value = 0; printf ("Please input dimension: "); scanf (" % d ", & dimension); P = (int *) malloc (sizeof (INT) * Dimension * dimension); startx = 0; starty = 0; endx = dimension-1; Endy = dimension-1; while (startx <= endx) {If (startx = endx) // only one vertex is left. The value {P [Endy * dimension + endx] = value ++; break;} for (I = starty, j = startx; I <= Endy; I ++) // from top to bottom {P [I * dimension + J] = value ++;} for (I-= 1, j = startx + 1; j <= endx; j ++) // from left to right, I has exceeded the matrix range in the input from top to bottom, so we need to subtract 1 {P [I * dimension + J] = value ++;} For (J-= 1, I-= 1; I> = starty; I --) // from bottom to top {P [I * dimension + J] = value ++;} for (I + = 1, J-= 1; j> starty; j --) // from right to left {P [I * dimension + J] = value ++;} startx ++; starty ++; endx --; Endy --;} for (I = 0; I <Dimension * dimension; I ++) {If (0 = I % dimension) printf ("\ n "); printf ("% 3d", P [I]);} printf ("\ n"); Return 0 ;}

Program running result:


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.