CS 163 Lecture-Module #02:
Multidimensional Arrays


basic array:
    +---+---+---+---+---+---+
    |   |   |   |   |   |   |
    +---+---+---+---+---+---+
what if want grid?
    +---+---+---+---+---+---+
    |   |   |   |   |   |   |
    +---+---+---+---+---+---+
    |   |   |   |   |   |   |
    +---+---+---+---+---+---+
    |   |   |   |   |   |   |
    +---+---+---+---+---+---+
    |   |   |   |   |   |   |
    +---+---+---+---+---+---+

multidimensional array

declaration:
elt._type[][] m_array_name;
e.g.   int[][] cell;

allocation:
m_array_name = new elt._type[num._columns][num._rows];
e.g.   
    cell[][]  :
       
       
       
       
       

(actually the interpretation of whether it's the first or the second coordinate that corresponds to a row or a column is a choice
machine gives simply  spaces
it doesn't force interp. horiz. by vert. or vert. by horiz.;
that's your choice)

usage:
multiple indexing to get individual elements
m_array_name[column_index][row_index]
variable of  elt._type
e.g.:
    cell[2][3] = -5;
    cell[0][4] = 8;
not comma-separated
    cell[2,3] = -5;    

underlying mechanisms explain that disallowment of commas
plus various other things you can or cannot do with multidimensional arrays
(not that we'll cover everything in this Lecture-Module)

actually 
rem. can have array of anything:
documentation specifies declaration should be "type [] array_name"
type could be "Color" or "boolean" or "StudentRecord" or ...
    Color [] ca;
    boolean [] ba;
    StudentRecord [] sra;
            .
            .
            .
well, type can be "int[]":
    (int[]) [] i2a;
thus   "(int[]) [] cell = new int [5] [4]"
means  cell  is an array of  subarrays  int[] subarray = new int[5]
cell[][] :

  cell[0] :     cell[1] :       cell[2] :       cell[3] :
    +---+         +---+           +---+           +---+
    |   |         |   |           |   |           |   |
    +---+         +---+           +---+           +---+
    |   |         |   |           |   |           |   |
    +---+         +---+           +---+           +---+
    |   |         |   |           |   |           |-5 |
    +---+         +---+           +---+           +---+
    |   |         |   |           |   |           |   |
    +---+         +---+           +---+           +---+
    | 8 |         |   |           |   |           |   |
    +---+         +---+           +---+           +---+
if a is an array, may use code  a[2]  ,  a.length
cell  is an array (of subarrays), so may use code  cell[2]  ,  cell.length  which is 
cell[2]  is an array (of  ints) --- subarray of  cell[][]  ---
so may use code  (cell[2])[3]  ,  (cell[2]).length  which is 
(parentheses there just for explication; they're really optional
standard: have just  cell[2][3],  cell[2].length )
e.g.:
for( int r = 0; r < (cell[2]).length; r++ )
  System.out.println( (cell[2])[r] );

for( int r = 0; r < (cell[0]).length; r++ ) {
  for( int c = 0; c < cell.length; c++ )
    System.out.print( (cell[c])[r] + " ");
  System.out.println();
  }
parentheses above just for explication;
standard: just have things as in the following:
for( int r = 0; r < cell[2].length; r++ )
  System.out.println( cell[2][r] );

for( int r = 0; r < cell[0].length; c++ ) {
  for( int c = 0; c < cell.length; c++ )
    System.out.print( cell[c][r] + " ");
  System.out.println();
  }


example program: Life
simulation
popularized "Scientific American"
John Conway
    in field related to John Nash of movie "A Beautiful Mind"
rel. Artificial Intelligence

grid of cells:
code specifies  SIDE  columns,  SIDE  rows
    cell[][]:
    +---+---+---+-------+---+
    |   |   |   |       |   |   row 0
    +---+---+---+-------+---+
    |   |   |   |       |   |   row 1
    +---+---+---+-------+---+
    |   |   |   |       |   |   row 2
    +---+---+---+-------+---+
    |   |   |   |       |   |   .
    |   |   |   |       |   |   .
    |   |   |   |       |   |   .
    +---+---+---+-------+---+
    |   |   |   |       |   |   
    +---+---+---+-------+---+
                                last row: 
col:  0   1   2    ...  
each cell with live 'Martian' critter painted red, dead/no critter painted blue
as time passes, the critters reproduce or die or simply survive
    depending on 
initially: random scattering of live critters
edge-cells: no food

cell-values
0 DEAD
1 ALIVE

rules for propagation:
if  cell[c][r]  DEAD but 3 neighbors ALIVE
        ⇒ next  cell[c][r]
if  cell[c][r]  ALIVE, then different possibilities as follows:
* 4 or more neighbors ALIVE
        ⇒    cell[c][r]  will die from overcrowding
* 0 or 1 neighbors ALIVE
        ⇒    cell[c][r]  will die from loneliness
* 2 or 3 neighbors ALIVE
        ⇒    cell[c][r]  will survive

so need to count neighbors ALIVE
but what are the cells neighboring  cell[c][r]  ?
cell[][]
      |
      |         |               |
      V         |               |
                |               |
----------------+---------------+---------------
                |               |
                |  cell[c][r]   |  cell[][]
                |               |
----------------+---------------+---------------
                |               |
                |               |
                |               |
count by


then the  switch  handles the different cases: numbers alive, death, whatever
'unifying' cases

doing 'calculation' of next generation in  next_cell[][]
when calculated, 



we have 'cells' cooperating, interdepending, movement,
'glider-gun' generates ...
Does this qualify as life?




can have arrays with more dimensions: more brackets etc.




summary of this lecture-module

basics of multidimensional arrays:


(Copyright © 2008 by Hugh McGuire   — for thoughts about this, see   http://www.cis.gvsu.edu/~mcguire/teaching/copyright_thoughts.html .)