Datasheet
The preceding example shows a one-dimensional array, which you can think of as a line of integers, each
with its own numbered compartment. C++ allows multidimensional arrays. You might think of a two-
dimensional array as a checkerboard, where each location has a position along the x-axis and a position
along the y-axis. Three-dimensional and higher arrays are harder to picture and are rarely used. The
code below shows the syntax for allocating a two-dimensional array of characters for a Tic-Tac-Toe board
and then putting an “o” in the center square.
char ticTacToeBoard[3][3];
ticTacToeBoard[1][1] = ‘o’;
Figure 1-1 shows a visual representation of this board with the position of each square.
Figure 1-1
Functions
For programs of any significant size, placing all the code inside of main() is unmanageable. To make
programs easy to understand, you need to break up, or decompose, code into concise functions.
In C++, you first declare a function to make it available for other code to use. If the function is used
inside a particular file of code, you generally declare and define the function in the source file. If the
function is for use by other modules or files, you generally put the declaration in a header file and the
definition in a source file.
In C++, the first element of an array is always at position 0, not position 1! The last
position of the array is always the size of the array minus 1!
ticTacToeBoard[0][0] ticTacToeBoard[0][1] ticTacToeBoard[0][2]
ticTacToeBoard[1][0] ticTacToeBoard[1][1] ticTacToeBoard[1][2]
ticTacToeBoard[2][0] ticTacToeBoard[2][1] ticTacToeBoard[2][2]
16
Chapter 1
04_574841 ch01.qxd 12/15/04 3:39 PM Page 16