Search This Blog

Saturday, 28 September 2013

array

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 statement

int Scores (10);

sets up an array called Scores, consisting of 10 integer values. 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.)

int * ptr;
ptr = &Scores [0];

(See pointeRs and indiRection.)

   Arrays are useful because they allow a program to work easily with a group of data items without having to use separately named variables. Typically, a program uses a loop to traverse an array, performing the same operation on each element in order (see loop). For example, to print the current 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 suppport multidimensional arrays. For example, a two-dimensional 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-dimensional array might hold four test scores for each person.

   Some languages such as FORTRAN 90 allow for defining “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, “Jackson” => 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 outof-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 inflexible, 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 (see heap). The program 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++).

   In the early days of microcomputer programming, arrays tended to be used as an all-purpose data structure for storing 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).

No comments:

Post a Comment