Use a single loop to initialize a multi-dimensional array
For programmers, the most commonly used multi-dimensional arrays are two-dimensional arrays and three-dimensional arrays. The method and essence of multi-dimensional arrays are similar. Here I will only discuss the initialization method, which method is used?
/*************************************** * ******************************* Copyright (c) 2015, WK Studios * Filename: * Compiler: GCC, VS, VC6.0 win32 * Author: WK * Time: 2015 3 29 ************************************* ********************************** // use the second time loop initialization array # include
# Include
# Define N 4 # define M 3int main () {int a [N] [M] = {0}; int I = 0, j = 0, n = 0; // clock_t start, stop; // start = clock (); for (I = 0; I
The result is as follows:
/*************************************** * ******************************* Copyright (c) 2015, WK Studios * Filename: * Compiler: GCC, VS, VC6.0 win32 * Author: WK * Time: 2015 3 29 ************************************* * ********************************* // use loop initialization array # include
# Include
# Define N 4 # define M 3int main () {int a [N] [M] = {0}; int I = 0, n = 0; // clock_t start, stop; // start = clock (); for (I = 0; I <(N * M); I ++) {a [I/N] [I % M] = ++ n; // I/N indicates the row in which I % M is located, the array elements are linearly stored in printf ("%-5d", a [I/N] [I % M]); if (I + 1) % N = 0) {printf ("\ n") ;}// stop = clock (); // printf ("time used: %-5f ", (double) (stop-start)/CLK_TCK); // printf ("\ n"); return 0 ;}
The result is as follows:
/*************************************** * ******************************* Copyright (c) 2015, WK Studios * Filename: * Compiler: GCC, VS, VC6.0 win32 * Author: WK * Time: 2015 3 29 ************************************* *********************************** // use it three times cyclically initialize 3D arrays # include
# Include
# Define N 3 # define M 4 # define P 5int main () {int a [N] [M] [P] = {0}; int I = 0, j = 0, k = 0, n = 0; clock_t start, stop; start = clock (); for (I = 0; I
The result is as follows:
/*************************************** * ******************************* Copyright (c) 2015, WK Studios * Filename: * Compiler: GCC, VS, VC6.0 win32 * Author: WK * Time: 2015 3 29 ************************************* * ********************************* // use cyclically initialize 3D arrays # include
# Include
# Define N 3 # define M 4 # define P 5int main () {int a [N] [M] [P] = {0}; int I = 0; clock_t start, stop; start = clock (); for (I = 0; I <(N * M * P); I ++) {a [I/(M * P)] [(I/P) % M] [I % P] = I; // I/(M * P) determine which side, (I/P) % M is determined in that row, I % P is determined in which column, and then combined with the array element is linear storage printf ("%-5d ", a [I/(M * P)] [(I/P) % M] [I % P]); if (I + 1) % P = 0) {printf ("\ n") ;}} stop = clock (); printf ("time used: %-5f", (double) (stop-start) /CLK_TCK); printf ("\ n"); return 0 ;}
The result is as follows: