Use a single loop to initialize a multi-dimensional array

Source: Internet
Author: User

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:

 


 

Related Article

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.