Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Multi-Dimensional Arrays

 

A multi-dimensional array  of dimension n (i.e., an n-dimensional array or simply n-D array) is a collection of items which is accessed via n subscript expressions. E.g., the tex2html_wrap_inline61089 element of the two-dimensional array x is accessed by writing x[i][j].

The C++ programming language provides built-in support for multi-dimensional arrays. However, the built-in multi-dimensional arrays suffer the same indignities that simple one-dimensional arrays do: Arrays in C++ are not first-class data types. There is no such thing as an array-valued expression. Consequently, you cannot use an array as an actual value parameter of a function; you cannot return an array value from a function; and, you cannot assign one array to another. The subscript ranges all start at zero and there is no bounds checking of array subscript expressions. Finally, the size of an array is static and fixed at compile time, unless dynamic memory allocation is explicitly used by the programmer.

In order to illustrate how these deficiencies of the C++ built-in multi-dimensional arrays can be overcome, we will examine the implementation of a two-dimensional array class, Array2D, that is based on the one-dimensional array class discussed in Section gif.




next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.