PROG

move some numbers around in an array--nothing useful

PED

Introduction to one-dimensional arrays

CONCEPTS

The array notation
identifier-name: array [ m..n] of xxxx;

creates several identical boxes. Each is of type xxx.

Use array[int-expr] on either left or right of :=
to refer to the box

int-expr, expression involving integers or integer constants.



Important Operations with 1-D arrays
1.   Copy a one-D array to another

2.   Write out a one-D array

3.   Read in a one-D array--keep track of last element

4.   Sum the array

5.   Take the minimum/maximum of an array

6.   Take the count of all elements that meet a certain condition

7.   Search for the first number in an array that meets a given condition

8.   Search for the last number in an array that meets a given condition

9.   Reverse an array 


In the list of boxes in the var statement we can include statements like
identifier-name: array [ m..n] of xxxx;

This creates several identical boxes. Each is of type xxx.

The first box will be referred to as
identifier-name[m]
The next one would be
identifier-name [m+1]
And so on until
identifier-name[n]

m and n are both integer constants and m should be less than n.

We can refer to the boxes by either putting an absolute number like identifier-name[3] or identifier-name[5].

However, much more useful would be writing something like identifier-name[int-expr] where int-expr is either an integer variable or some expression involving integer variables and constants.

As an analogy, we can think of the array, identifier-name as a set of post office boxes.

When we put the identifier-name[int-expr] in the left hand side of
expression, e. g.
ax[3]:=.....

Then that is like going to Post Office Box #3 and putting something into it.

When we have it on the right-hand side of the expression, then that is
like inspecting--but not removing--the postcard in the box.

That is,
...:=....+ax[3]....

is like inspecting the number on the slip of paper in P.O.B. #3.

Each declaration of an array var part, creates a new set of Post Office Boxes and gives the entire set a name.

In our example, on line 2, we create a "Post Office Box Set" called
leff. The individual elements are referred to by:

leff[1], leff[2], leff[3], leff[4], leff[5], leff[6], leff[7] , leff[8]

In line 3, we also create a set called cooper; these elements would be referred to as:
cooper[161], cooper[162], cooper[3], cooper[4], cooper[5], cooper[6], cooper [7].

See the figure to see how these are arranged.

Line 7 puts the first (leftmost) box of the array "leff"

LIne 8 sets the leftmost box of the array cooper to 19--which is numbered 161.

Lines 9, 10 and 11 set i, j, and k to the values 1, 162 and 3. We will use these to illustrate how variable names can be included in the subscript to an array.

LInes 12 and 13 set the next position (from the left) of leff and cooper to 162 and 3 respectively.

Line 14 is the first example where a variavble name is in the subscript position (between the left and right bracket). Since k is 3, the 161 is put in position three of leff.

In line 16, j is changed from 162 to 163.

Thus line 16, will change element 163 of cooper to four (k+1)

Line 17 sets position four of leff to 163.

Line 18 sets copper[164] to five.

Line 19 sets z to two.

Line 20 illustrates a two-variable expression in the subscript part. The comptuer adds k and z together getting five. Then it puts six into element five of leff.

Similarly, in line 21, 163+2 is added togerther to get the subscript for cooper and element 165 is set equal to 3-1 or two.

In lines 22 and 23, we get the value of leff[2] into temp1 giving it the value 162. Then it is put into leff[6].

In line 24, we show how movement from one array location to another can occur in a single statement. Element 162 of cooper is picked up for the right hand side of the assignment statement. This is element number 3. Then, this is put into element 163+3 of cooper on the left hand side.

In line 25, we show one of several examples that show subscripted variables can themselves serve as subscripts to an array. These might look a little confusing at first, but if you work them out systematically from the inside out, you will be able to get the right answer.

cooper[j] is caluclated giving us 4, the contents of the element cooper[163]. Then we take the fourth element of leff which just happens to be the value 164 as well.

Let's see this again in line 27. We start by taking the value of leff[z], or the second lement of leff. This is 162. WE then take the contents cooper[162] and put this in temp2.

Line 29 gives us another one. Let's start by computing cooper[j] and cooper[j+k] These are 4 and 3, respecively. Then we add these to get seven. This tells us to put 99 into element 7 of the array.

Line 30 shows us still another, just a tad more complicated. WE start by computing the innermost subscript, cooper[j+1]. This is 5. Then we compute leff [k] and leff[5]--which are 161 and 6, respectively.

Then, we put the 101 in element 167 of cooper.

The remaining lines show you that array subscripting can be done inside a loop. In fact, most work with arrays is done with loops of various kinds.

Lines 33 to 38 print out the successive elements of the leff array. i starts at one. So the first time through the loop, leff[1] gets printed. Then i becomes 2, and line 35 will print out leff[2]. And so on, as i goes through the values 3, 4, 5, 6, and 7, the contents of leff[3], leff[4], leff[5], leff[6] and leff[7] are all printed out.

Likewise, lines 40 through 45 will make i take on the values 161, 162, 163, ..., 167. And cooper[161], cooper[162], cooper[163],.... cooper[167] will be successively printe dout.

These loops that process each element of an array sequentially from one element to the last elemetn occur very often in programming. PASCAL provides a special syntax sugar for it called a "for" loop that makes this kind of loop shorter. We will see this in Unit 5.1 supplementary, as well as more examples of where we go sequentially through an array.