An array stores a group of similar data items in consecutive
order. Each item is an element of the array, and it can be
retrieved using a subscript that specifies the item’s location
relative to the first item. Thus in the C language, the state-
ment
int Scores (10);
sets up an array called Scores, consisting of 10 integer val-
ues. The statement
Scores [5] = 93;
stores the value 93 in array element number 5. One subtlety,
however, is that in languages such as C, the first element of
the array is [0], so [5] represents not the fifth but the sixth
element in Scores. (Many version of BASIC allow for setting
either 0 or 1 as the first element of arrays.)
In languages such as C that have pointers, an equivalent
way to access an array is to declare a pointer and store the
address of the first element in it:
int * ptr;
ptr = &Scores [0];
Arrays are useful because they allow a program to work
easily with a group of data items without having to use sep-
arately named variables. Typically, a program uses a loop to
traverse an array, performing the same operation on each
element in order.For example, to print the cur-
rent contents of the Scores array, a C program could do the
following:
int index;
for (index = 0; i < 10; i++)
printf (“Scores [%d] = %d \n”, index,
Scores [index]);
This program might print a table like this:
Scores [0] = 22
Scores [1] = 28
Scores [2] = 36
and so on. Using a pointer, a similar loop would increment
the pointer to step to each element in turn.
An array with a single subscript is said to have one
dimension. Such arrays are often used for simple data lists,
strings of characters, or vectors. Most languages also sup-
port multidimensional arrays. For example, a two-dimen-
sional array can represent X and Y coordinates, as on a
screen display. Thus the number 16 stored at Colors[10][40]
might represent the color of the point at X=10, Y=40 on a
640 by 480 display. A matrix is also a two-dimensional
array, and languages such as APL provide built-in support
for mathematical operations on such arrays. A four-dimen-
sional array might hold four test scores for each person.
Some languages such as FORTRAN 90 allow for defin-
ing “slices” of an array. For example, in a 3 × 3 matrix, the
expression MAT(2:3, 1:3) references two 1 × 3 “slices” of the
matrix array. Pascal allows defining a subrange, or portion
of the subscripts of an array.
Associative Arrays
It can be useful to explicitly associate pairs of data items
within an array. In an associative array each data element
has an associated element called a key. Rather than using
subscripts, data elements are retrieved by passing the key
to a hashing routine (see hashing). In the Perl language, for
example, an array of student names and scores might be set
up like this:
%Scores = (“Henderson” => 86, “Johnson” => 87, “Jack-
son” => 92);
The score for Johnson could later be retrieved using the
reference:
$Scores (“Johnson”)
Associative arrays are handy in that they facilitate look-up
tables or can serve as small databases. However, expanding
the array beyond its initial allocation requires rehashing all
the existing elements.
Programming Issues
To avoid error, any reference to an array must be within
its declared bounds. For example, in the earlier example,
Scores[9] is the last element, and a reference to Scores[10]
would be out of bounds. Attempting to reference an out-
of-bounds value gives an error message in some languages
such as Pascal, but in others such as standard C and C++, it
simply retrieves whatever happens to be in that location in
memory.
Another issue involves the allocation of memory for the
array. In a static array, such as that used in FORTRAN 77,
the necessary storage is allocated before the program runs,
and the amount of memory cannot be changed. Static arrays
use memory efficiently and reduce overhead, but are inflex-
ible, since the programmer has to declare an array based
on the largest number of data items the program might be
called upon to handle. A dynamic array, however, can use a
flexible structure to allocate memory. The pro-
gram can change the size of the array at any time while it
is running. C and C++ programs can create dynamic arrays
and allocate memory using special functions (malloc and
free in C) or operators (new and delete in C++).
A two-dimensional array can be visualized as a grid, with the
array subscripts indicating the row and column in which a par-
ticular value is stored. Here the value 4 is stored at the location
(1,2), while the value at (2,0), which is 8, is assigned to N. As
shown, the actual computer memory is a one dimensional line
of successive locations. In most computer languages the array is
stored row by row.
In the early days of microcomputer programming, arrays
tended to be used as an all-purpose data structure for stor-
ing information read from files. Today, since there are more
structured and flexible ways to store and retrieve such data,
arrays are now mainly used for small sets of data (such as
look-up tables).
0 comments:
Post a Comment