For programmers, the most commonly used multidimensional arrays are two-dimensional arrays and three-dimensional arrays, for more dimensions of the array method and the essence are similar, I am just here to discuss the method of initialization, exactly which method to use, but also because of the different
/********************************************************************** * Copyright (c) 2015,wk Studios* Filename: * compiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * time:2015 3 29******************************* ///With two cycles initialize array #include<stdio.h> #include <time.h> #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<n;i++) { for (j=0;j<m;j++) {a[i][j]=++n;printf ("%-5d", A[i][j]);} printf ("\ n");} Stop=clock ();//printf ("Time used:%-5f", ((double) (Stop-start)/clk_tck));//printf ("\ n"); return 0;}
The results are as follows
/********************************************************************** * Copyright (c) 2015,wk Studios * Filename: * compiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * time:2015 3 29 ******************************* Initialize an array with a single loop #include<stdio.h> #include <time.h> #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 determines in which row, I%m determines in that column, and then combines the array elements with the linear storage of 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));
The results are as follows
/********************************************************************** * Copyright (c) 2015,wk Studios* Filename: * compiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * time:2015 3 29******************************* ///With three cycles initialize the three-dimensional array #include<stdio.h> #include <time.h> #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<n;i++) {for (j=0;j<m;j++) {for (k=0;k<p;k++) {a[i][j][k]=++n;printf ("%-5d", A[i][j][k]);} printf ("\ n");} printf ("\ n");} Stop=clock ();p rintf ("Time Taken:%-5f", ((double) (Stop-start)/clk_tck));p rintf ("\ n"); return 0;}
The results are as follows:
/********************************************************************** * Copyright (c) 2015,wk Studios* Filename: * compiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * time:2015 3 29******************************* Initialize a three-dimensional array with a single loop #include<stdio.h> #include <time.h> #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) determines on which side, (i/p)%M determines in that row, i%p determines in which column, and then combines the array elements with the linear storage of printf ("%-5d", a[i/(M*p) [(i/p)%m][i%p]); if (i+1)%p== 0) {printf ("\ n");}} Stop=clock ();p rintf ("Time Taken:%-5f", ((double) (Stop-start)/clk_tck));p rintf ("\ n"); return 0;}
The results are as follows:
Initializing a multidimensional array with a single loop