20.03.2013 Views

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

NOTES ON DATA STRUCTURING 115<br />

6. TIlE ARRAY<br />

The array is for many programmers the most familiar data structure, and in<br />

some programming languages it is the <strong>on</strong>ly structure explicitly available.<br />

From the abstract point of view, an array may be regarded as a mapping<br />

between a domain of <strong>on</strong>e type (the subscript range) and a range of some<br />

possibly different type (the type of the array, or more accurately, the type of<br />

its elements).<br />

The type of a mapping is normally specified by a mathematician using an<br />

arrow:<br />

M: D ~ R;<br />

where D is the domain type and R is the range type. An alternative notati<strong>on</strong><br />

which will be more familiar to programmers is"<br />

M:array D of R.<br />

This notati<strong>on</strong> is more expressive of the manner in which the data is repre-<br />

sented, whereas the mathematical notati<strong>on</strong> emphasises the abstract character<br />

of the structure, independent of its representati<strong>on</strong>.<br />

When a particular value M of a mapping type is applied to a value x of the<br />

domain type, it specifies some unique element of the range type, which is<br />

known as M of x, and is written using either round or square brackets<br />

M(x) or Mix].<br />

Another name for a mapping is a functi<strong>on</strong>" the term "mapping" is used to<br />

differentiate the data structure from a piece of program which actually<br />

computes a value in its range from an argument in its domain. The essence of<br />

the difference is that a mapping M is specified not by giving a computati<strong>on</strong><br />

method but by explicitly listing the value of M(x) for each possible value x<br />

in its domain. Thus an array can be used <strong>on</strong>ly for functi<strong>on</strong>s defined at a<br />

finite set of points, whereas the domain of a computed functi<strong>on</strong> may be<br />

infinite.<br />

An example of a finite mapping is a m<strong>on</strong>thtable, which specifies for each<br />

m<strong>on</strong>th of the year the number of days it has:<br />

type m<strong>on</strong>thtable = array m<strong>on</strong>th of 28.. 31.<br />

The domain is the m<strong>on</strong>th type and the range type c<strong>on</strong>sists of the integers<br />

between 28 and 31 inclusive. A typical value of this tyiae may be simply<br />

specified by listing the values of M(x) as x ranges over its domain. Thus<br />

if M: m<strong>on</strong>thtable is specified as<br />

m<strong>on</strong>thtable (Jan: 31, Feb: 28, March: 31, April: 30,<br />

May: 31, June: 30, July: 31, Aug: 31,<br />

Sept: 30, Oct: 31, Nov: 30, Dec: 31)<br />

then M[Jan] = 31, M[Feb] = 28, and so <strong>on</strong>.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!