0% found this document useful (0 votes)
63 views

Chapter 3: Arrays: I. The Array Structure

The document discusses arrays, which are a basic structure for storing and accessing collections of data. Arrays allow for random access to individual elements through indexes and are widely used in computer science problems where the number of elements is known beforehand. The document also covers the importance of arrays versus lists, noting that arrays are better for fixed sized collections while lists allow more flexible operations but have extra unused storage space.

Uploaded by

Carlton Afuhnwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Chapter 3: Arrays: I. The Array Structure

The document discusses arrays, which are a basic structure for storing and accessing collections of data. Arrays allow for random access to individual elements through indexes and are widely used in computer science problems where the number of elements is known beforehand. The document also covers the importance of arrays versus lists, noting that arrays are better for fixed sized collections while lists allow more flexible operations but have extra unused storage space.

Uploaded by

Carlton Afuhnwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter

3: Arrays
The most basic structure for storing and accessing a collection of data is the array.
Arrays can be used to solve a wide range of problems in computer science. Most programming
languages provide this structured data type as a primitive and allow for the creation of arrays
with multiple dimensions.

I. The array structure


At the hardware level, most computer architectures provide a mechanism for creating and
using one-dimensional arrays. A one-dimensional array, as illustrated in Figure 2.1, is
composed of multiple sequential elements stored in contiguous bytes of memory and allows for
random access to the individual elements. The entire contents of an array are identified by a
single name. Individual elements within the array can be accessed directly by specifying an
integer subscript or index value, which indicates an offset from the start of the array. This is
similar to the mathematics notation (xi), which allows for multiple variables of the same name.
The difference is that programming languages typically use square brackets following the array
name to specify the subscript, x[i].

Figure 2 A sample 1-D array consisting of 11 elements.

The array is probably the most widely used data structure; in some languages it is even
the only one available. An array consists of components which are all of the same type, called
its base type; it is therefore called a homogeneous structure. The array is a random-access
structure, because all components can be selected at random and are equally quickly accessible.
In order to denote an individual component, the name of the entire structure is augmented by
the index selecting the component. This index is to be an integer between 0 and n-1, where n is
the number of elements, the size, of the array.
TYPE T = ARRAY n OF T0
Examples
TYPE Row = ARRAY 4 OF REAL
TYPE Card = ARRAY 80 OF CHAR
TYPE Name = ARRAY 32 OF CHAR
A particular value of a variable
VAR x: Row
with all components satisfying the equation xi = 2−i, may be visualized as shown in Figure 3.

11
Figure 3 Array of type Row with xi = 2−i.

An individual component of an array can be selected by an index. Given an array


variable x, we denote an array selector by the array name followed by the respective
component’s index i, and we write xi or x[i]. Because of the first, conventional notation, a
component of an array component is therefore also called a subscripted variable.
The common way of operating with arrays, particularly with large arrays, is to
selectively update single components rather than to construct entirely new structured values.
This is expressed by considering an array variable as an array of component variables and by
permitting assignments to selected components, such as for example x[i]:= 0.125. Although
selective updating causes only a single component value to change, from a conceptual point of
view we must regard the entire composite value as having changed too.
The fact that array indices, i.e., names of array components, are integers, has a most
important consequence: indices may be computed. A general index expression may be
substituted in place of an index constant; this expression is to be evaluated and the result
identifies the selected component. This generality not only provides a most significant and
powerful programming facility, but at the same time it also gives rise to one of the most
frequently encountered programming mistakes: The resulting value may be outside the interval
specified as the range of indices of the array. We will assume that decent computing systems
provide a warning in the case of such a mistaken access to a non-existent array component.
The cardinality of a structured type, i.e. the number of values belonging to this type, is
the product of the cardinality of its components. Since all components of an array type T are
of the same base type T0, we obtain
card(T) = card(T0)n
Constituents of array types may themselves be structured. An array variable whose components
are again arrays is called a matrix. For example,
M: ARRAY 10 OF Row
is an array consisting of ten components (rows), each constisting of four components of type
REAL, and is called a 10*4 matrix with real components. Selectors may be concatenated
accordingly, such that Mij and M[i][j] denote the jth component of row Mi, which is the ith
component of M. This is usually abbreviated as M[i, j] and in the same spirit the declaration
M: ARRAY 10 OF ARRAY 4 OF REAL
can be written more concisely as
M: ARRAY 10, 4 OF REAL.
If a certain operation has to be performed on all components of an array or on adjacent
components of a section of the array, then this fact may conveniently be emphasized by using
the FOR statement, as shown in the following examples for computing the sum and for finding
the maximal element of an array declared as

12
VAR a: ARRAY N OF INTEGER
sum:= 0;
FOR i:= 0 TO N-1 DO sum:= a[i] + sum END
k:= 0; max:= a[0];
FOR i:= 1 TO N-1 DO
IF max < a[i] THEN k:= i; max:= a[k] END
END.
In a further example, assume that a fraction f is represented in its decimal form with k-1 digits,
i.e., by an array d such that
f=∑ ∗ or
f= + 10 ∗ + 100 ∗ + . . . + ∗ 10
Now assume that we wish to divide f by 2. This is done by repeating the familiar division
operation for all k-1 digits di, starting with i=1. It consists of dividing each digit by 2 taking
into account a possible carry from the previous position, and of retaining a possible remainder
r for the next position:
: = 10 ∗ + [ ];
[ ]: = 2;
:= 2
This algorithm is used to compute a table of negative powers of 2. The repetition of halving to
compute 2−1, 2−2, ..., 2−N is again appropriately expressed by a FOR statement, thus leading to
a nesting of two FOR statements.
PROCEDURE Power(VAR W: Texts.Writer; N: INTEGER);
(*compute decimal representation of negative powers of 2*)
VAR i, k, r: INTEGER;
d: ARRAY N OF INTEGER;
BEGIN
FOR k := 0 TO N-1 DO
Texts.Write(W, "."); r := 0;
FOR i := 0 TO k-1 DO
r := 10*r + d[i]; d[i] := r DIV 2; r := r MOD 2;
Texts.Write(W, CHR(d[i] + ORD("0")))
END ;
d[k] := 5; Texts.Write(W, "5"); Texts.WriteLn(W)
END
END Power.
The resulting output text for N = 10 is
.5
.25
.125
.0625
.03125
.015625
.0078125
.00390625
.001953125
.0009765625

13
II. The importance of arrays
You will notice the array structure looks very similar to list structure. That's because the
two structures are both sequences that are composed of multiple sequential elements that can
be accessed by position. But there are two major differences between the array and the list.
First, an array has a limited number of operations, which commonly include those for array
creation, reading a value from a specific element, and writing a value to a specific element. The
list, on the other hand, provides a large number of operations for working with the contents of
the list. Second, the list can grow and shrink during execution as elements are added or removed
while the size of an array cannot be changed after it has been created.
You may be wondering, if the list structure is a mutable sequence type, why are we
bothering to discuss the array structure, much less plan to implement an abstract data type for
working with arrays? The short answer is that both structures have their uses. There are many
problems that only require the use of a basic array in which the number of elements is known
beforehand and the flexible set of operations available with the list is not needed.
The array is best suited for problems requiring a sequence in which the maximum
number of elements are known up front, whereas the list is the better choice when the size of
the sequence needs to change after it has been created. As you will learn later in the chapter, a
list contains more storage space than is needed to store the items currently in the list. This extra
space, the size of which can be up to twice the necessary capacity, allows for quick and easy
expansion as new items are added to the list. But the extra space is wasteful when using a list
to store a fixed number of elements. For example, suppose we need a sequence structure with
100; 000 elements. We could create a list with the given number of elements
using the replication operator:
values = [ None ] * 100000

But underneath, this results in the allocation of space for up to 200,000 elements, half of which
will go to waste. In this case, an array would be a better choice. The decision as to whether an
array or list should be used is not limited to the size of the sequence structure. It also depends
on how it will be used. The list provides a large set of operations for managing the items
contained in the list. Some of these include inserting an item at a specific location, searching
for an item, removing an item by value or location, easily extracting a subset of items, and
sorting the items. The array structure, on the other hand, only provides a limited set of
operations for accessing the individual elements comprising the array. Thus, if the problem at
hand requires these types of operations, the list is the better choice.

III. The Array Abstract Data Type


The array structure is commonly found in most programming languages as a primitive type, but
some languages like Python only provides the list structure for creating mutable sequences.
A one-dimensional array is a collection of contiguous elements in which individual elements
are identified by a unique integer subscript starting with zero. Once an array is created, its size
cannot be changed. The following operations can be performed on an array:
 Array( size ): Creates a one-dimensional array consisting of size elements with each
element initially set to None. size must be greater than zero.

14
 length (): Returns the length or number of elements in the array.
 getitem ( index ): Returns the value stored in the array at element position index. The
index argument must be within the valid range. Accessed using the subscript operator.
 setitem ( index, value ): Modi_es the contents of the array element at position index to
contain value. The index must be within the valid range. Accessed using the subscript
operator.
 clearing( value ): Clears the array by setting every element to value.
 iterator (): Creates and returns an iterator that can be used to traverse the elements of
the array.
Some computer scientists consider the array a physical structure and not an abstraction since
arrays are implemented at the hardware level. But remember, there are only three basic
operations available with the hardware-implemented array. As part of our Array ADT, we have
provided for these operations but have also included an iterator and operations for obtaining the
size of the array and for setting every element to a given value. In this case, we have provided
a higher level of abstraction than that provided by the underlying hardware-implemented array.

15

You might also like