PROG
Move some Numbers Around as indicated
PED
To learn Two-Dimensional Arrays
CONCEPT
array-name:array [a..b,c..d] of xxxx
creates a two-dimensional array of type xxxx
First row is numbered a
Last row is numbered b
First column of each row is numbered c
Last column of each row is numbered d
array-name[int-expr1,int-expr2]
refers to element at
row: int-expr1
column: int-expr2
We can also consider a..b, c..d some other type of range
Should know how to take min, sum, etc.
of single row
of single column
of entire row
Should be able to print out entire array.
{ TEMPLATE A
process each element of row of array-name
row number given by row-number }
FOR J:= c TO d DO BEGIN
process array-name[row-number,J]
END;
{ TEMPLATE B
process each element of column of array-name
column number given by column-number
FOR I:= a TO b DO BEGIN
process array-name[I,column-number]
END;
{ TEMPLATE C
process all the elements of array-name
row first }
FOR I:= a TO b DO BEGIN
FOR J: = c TO d DO BEGIN
process array-name[I,J]
END;
END:
{ TEMPLATE D
process all elements of array-name
columns first }
FOR J := c TO d DO BEGIN
FOR I:= a TO b DO BEGIN
process array-name [I,J]
END;
END:
As shown by the figure, we can view a two-dimensional array as a
collection of rows and columns.
The two subscripts can be considered to designate the row and
column. The first subscript, the one before the comma is by
convention considered to designate a row and the second one
considered to designate the column. This follows the mathematical
convention used to refer to elements of matrices.
That is array[3,5] means the fifth column of the third row.
Array[i,j]
refers to the jth column of the ith row.
When we dimension the array--that is mention it in the "var" part, the array, we must provide the first and last row number and the first and last column number.
Thus, the dimensioning:
var a:array [1..5,1..3] of integer;
creates an array with five rows, each of which has three boxes or columns.
LIkewise, he dimensioning:
var bx:array [-2..2,1000.1005] of integer;
means that array bx has five rows. The first row is numbereed -2. The next one is numbered -1 and so on up to the last row which is numbered 2.
Each row has five columns. The first column is numbered 1000. The next one is numbered 1001. The last one is numbered 1005.
In general, I will say that we dimension or declare an array by writing the following in the var section.
var array-name [a..b,c..d] of integer;
This would mean that array-name would have rows designated a,a+1,... b and columns designated c,c+1, ... d.
Sometimes, it is convenient to consider the subscripts as not rows and columns but simply designators.
For example, we might have an array that represented the rainfalls for each of the twelve months for the years 1988 to 1992. We would write the dimensioning in the var part as:
var
Rainfall:array[1..12,1988..1992]
The first subscript would be the month number and the second would be the year.
Thus, we might write
Rainfall[4,1990]
for the rainfall in April of 1990. Note that April is the fourth month of the year.
Generally,
Rainfall[i,j]
would refer in the ith month of the jth year.
It is possible to have three or even more subscripts to an array. One limitation to the use of arrays with dimensions greater than two is the sheer number of elements they require, and the amount of physical storage in computers. To illustrate, an array declared ...[1..10,1..10,1..10] would have a thousand elements and one declared ...[1..100,1..100,1..100] would have a million elements. This latter array would not fit on many systems, particularly the IBM PC's on which Turbo Pascal runs.
Just as for one-dimensional arrays, most of the work in programs with two-dimensinonal arrays will involve for-loops. We will see that programs to process all the elements of a two-dimensional array, will use nested for loops.
Page 3 illustrates the for loops for some common tasks involving two-dimensional arrays.
Templates A and B show how to process a SINGLE row or column of a two-dimensional array. Template A will "process" the row number given by row-number. row-number may be an integer as in process row number three, or it may be specified as a variable or expression. By "process" we mean such as operations as reading or writing the contents of the element of the two-dimensional array. Or we could include the value in a sum, maximum or other similar property. Note that the variable J goes from from C to D. Remember from the above discussion that C is the number of the leftmost or first column and D is the number of the rightmost. Note also that we put the variable J in the second subscript--column numbers, by convention, are the second subscript.
Template B shows how we process a single column of an array. I use column-number to specify which column I wish to process; again that might be an integer, a variable or some expression. Observe that I have I go from a, the top-numbered row, to b, the number of the bottom row.
Templates C and D will both process every single element of the two-dimensional array, array-name. Template C does it row first. That is, it will process each element of the top-most (or first) row, then each element of the next row down, and so on until it processes each element of the last row.
I will indicate which row we are processing. Note that goes from a to b, the ranges given in the var statement for the row number. Observe also that J goes from c to d, the range given for the column numbers. And observe the "process array-name" statement. I, the variable indicating which row to use goes in the first subscript position. J, which indicates the column, ends up in the second subscript position of array-name.
Note that the loop with J is INSIDE the the loop of I. This is what ensures that we do the elements in row-order. One way to think of it is that I goes through each row number from a to b. We include in these loop, the template B that will process each element of the row.
Template D processes each element of the array--but it works by column. It processes the left-most column, then the next column, one to the right of the border and so on, until it processes the last column. Observe that this time, J, is in the outer loop--it still tells which column we are processing. The inner loop, using I, tells which row of that column is beign processed. Again, observe that this inner loop is simply template B, processing the column-number indicated by J.
It is now time to look at the example program.
Three arrays are created. They are called X, Y, and Z. They are dimensioned (on lines 3 to 5) as follows:
| first (top) row number a | last (bottom) row number b | first (left) column number c | last (rightmost) column number d | |
| X | 1 | 3 | 1 | 4 |
| Y | 1 | 3 | 1 | 4 |
| Z | 161 | 163 | 201 | 204 |
The lines 19 to 21 set row 2. It uses the Template A where column number is simply "2"
The lines 25 to 27 use template A again--this time to set the third row. Observe that we use "k" for column-number.
Liens 29 to 37 print out the array by row using template C. Here process array-name is done by the two write statements on lines 34 to 35.
In the lines 39 to 82, we show the templates to process things by columns. We do this to array Y, which is dimensioned the same as X.
Lines 43 to 45 set the first column of Y. Observe that each statement has a "1" in the second subscript--specifying column one.
Lines 50 to 52 set the second column of Y. Observe that each second subscript is either 2 or zing, a variable set to two.
Lines 57 to 59 use template B to set all the elements of column 3 to equal one. Observe that column-number is specified as 3. And lines 63 to 64 set the fourth column to equal the values 1 through 3. NOte here that column-number is k+blah, an expression whose value is four.
Now, we show the use of template c, printing otuthe arrays by row, and template d which shows us printing the array out by column. Observe the difference in the output on page 9.
The remainder of the program fills up array z. Observe that array Z contains 3 rows and four columns, just like Y. However, the rows and columns are numbered differently. a and b are 161 and 163 and c and d are 201 to 204. In spite of this the lines 90 to 138 will fill up the array Z so the picture looks the array so the numbers are in the same relative places. See drawing.
1: 2-d arrays filled by Unit 5.2 Example Program
LInes 91 to 93 set the first column and lines 98 to 101 set column 2.
In lines 105 to 107, we see template B which will set the third column from the left, numbered 203. Observe that cvolumn-number is 203 and observe that a and b are substituted with 161 and 163.
And in line 114, observe that we have the colun-number in template B eplaced with "rotgut"
Also observe, in the two loops to print out the arrays, that a, b, c and d are replaced with the values 161, 163, 201 and 204.