Difference between revisions of "Multidimensional arrays"

From Tux
Jump to: navigation, search
(Created page with "'''Multidimensional arrays''' in C++ are no more complicated than one-dimensional arrays. Note the following: <source lang="cpp"> int a; // here, a is how to access...")
 
 
Line 7: Line 7:
  
 
int b[10];      // here, b[0] is how to access one integer
 
int b[10];      // here, b[0] is how to access one integer
int b[10][10];  // now, b[0][0] through b[0][9] are how to access the elements "inside" b
+
int b[10][10];  // now, b[0][0] through b[0][9] are how to access the elements "inside" b[0]
 
</source>
 
</source>
  

Latest revision as of 13:35, 27 January 2018

Multidimensional arrays in C++ are no more complicated than one-dimensional arrays. Note the following:

int a;          // here, a is how to access one integer
int a[10];      // now, a[0] through a[9] are how to access the elements "inside" a


int b[10];      // here, b[0] is how to access one integer
int b[10][10];  // now, b[0][0] through b[0][9] are how to access the elements "inside" b[0]

Iterating through a two-dimensional array is accomplished by nesting loops:

int a[10];

for (int i = 0; i < 10; i++)
     a[i] = somevalue;



int b[3][5];

for (int i = 0; i < 3; i++)
     for (int j = 0; j < 5; j++)
          b[i][j] = somevalue;

The concepts described above for two-dimensional arrays apply to higher dimensional arrays as well.