|
|
|
|
|
|
|
Data structures play an important role in the design process. The choice of data structure directly affects the design, because it determines the algorithms used to process the data. We have discussed the one-dimensional array, which gives us the ability to reference a group of data objects by one name. This simplifies the design of many algorithms. |
|
|
|
|
|
|
|
|
In many problems, however, the relationships between data items are more complex than a simple list. In this chapter, we begin by examining the two-dimensional array, which is useful when data is to be organized in the form of a table with rows and columns. Two-dimensional arrays are also useful for representing board games, like chess, tic-tac-toe, or Scrabble, and in computer graphics, where the screen is thought of as a two-dimensional array. |
|
|
|
|
|
|
|
|
The definition of an array then is extended to allow arrays with any number of dimensions, called multidimensional arrays. Each dimension of such an array is used to represent a different feature of a component. For example, a three-dimensional array of sales figures might be indexed by (1) store number, (2) month, and (3) item number. |
|
|
|
|
|
|
|
|
A one-dimensional array is used to represent a list. A two-dimensional array is used to represent a table with rows and columns, provided each item in the table is of the same data type. A component in a twodimensional array is accessed by specifying the row and column indices of the item in a table. This is a familiar task. For example, if you want to find a street on a map, you look up the street name on the back of the map to find the coordinates of the street, usually a letter and a number. The letter specifies a column to look on, and the number specifies a row. You find the street where the row and column meet. |
|
|
|
 |
|
 |
|
|
Two-Dimensional Array A collection of components, all of the same type, structured in two dimensions. Each component is accessed by a pair of indices that represent the component's position in each dimension. |
|
|
|
|
|
|
|
|
Figure 13-1 shows a two-dimensional array that has 100 rows and 9 columns. The rows are accessed by an integer ranging from 0 through 99; the columns are accessed by an integer ranging from 0 through 8. Each component is accessed by a row-column pairfor example, 0, 5. |
|
|
|
|
|
|
|
|
A two-dimensional array is declared in exactly the same way as a onedimensional array, except that sizes must be specified for two dimensions. Below is the syntax template for declaring an array with more than one dimension, along with an example. |
|
|
|
|
|