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: