Erin's CS stuff

CS stuff!

2D Arrays

2D Arrays allow us to store multiple values that also have multiple dimensions.

A good example for 2D arrays is the game board for TicTacToe, where there are 9 spaces to choose from laid out in a grid with 3 across and 3 down.

Since we play with Xs and Os, it makes sense that we'll want to save each of those as char variables, which also means that a "blank" or empty space will need to be represented by a char value. I think a literal space ' ' would make the most sense!

Syntax

Declaration

2D array syntax is similar to (1D) array syntax, with one important difference: an additional size. The C syntax for a 2D array declaration statement must include a data type, an identifier (or name), a row size (between the first square brackets), a column size (between the second square brackets), and end with a semicolon.

For example, for our TicTacToe board, we can declare a 2D array:
char board[3][3];

Accessing Elements

We will still need to access each element one at a time. To do that, we'll use the square brackets again but instead of having the sizes inside, it will be the row and column index.

WARNING! Index values still always start at 0. They do not start at 1 (and should almost never be used that way).

For example, we can access the middle space through the following expression:
board[1][1]

To update the board to save player moves, we would use the assignment operator:

board[1][1] = 'X';
board[0][1] = 'O';

Accessing All Elements

Since the for loops we've been using with (1D) arrays would only access the elements in a single row, to access all of the elements in a 2D array we would need to repeat that process for every row...

...with another for loop! This

for(int rowIndex = 0; rowIndex < size; rowIndex++){
  for(int colIndex = 0; colIndex < size; colIndex++){
    board[rowIndex][colIndex] = ' ';
  }
}
clears the board by setting every element to the space character.

Behavior

Just like variables and 1D arrays, there is no visible program behavior associated with 2D array declaration. However, it can be very useful to understand what's going on under the hood!

Often, when we're in the abstraction or design phase of solving problems, we imagine or even draw 2D arrays with the 2 dimensions that apply to our problem (like the TicTacToe board above), but that's not how they're actually stored when using C. Instead, the operating system ensures that all of the addresses are still contiguous (or in a row) even though we imagine them on different rows. The compiler then does a bunch of simple arithmetic to ensure we're accessing the correct element with the two necessary indexes.

To access the element m[rowIndex][colIndex], the compiler must:

1. multiply rowIndex by the size of a single row of m and add this result to m's address to find the beginning of the correct row
 i. (the size of the single row would be the product of the number of columns and the size of an array element)
2. multiply colIndex by the size of an array element
3. add the previous result to the address from Step 1. to find the correct element