Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Moving though multi-dimensioned arrays

If you remember, or look back to searching and sorting single-dimensioned arrays you will know that to drive the inspection or sorting algorithm some kind of loop is required; this may be either a for, while or repeat - until loop. For single-dimension arrays a single loop is required, for a two dimensional array two loops are required. Using multiple loops together to drive through a multi-dimension array is called nesting loops. You probably could write an algorithm yourself now to run through a single-dimensional integer array and add up the values contained within it. Your algorithm might look something like this:

{ totals values contained in array } total := 0; FOR I := 1 TO MAX DO total := total + integer_array[I]; WRITELN('The total equals : ',total);

I hope you would admit that this was a fairly straightforward piece of coding to come up with. Well it is, and all that is needed to do the same job on a two-dimension array is to put this algorithm inside another FOR loop. It is very much like working with an array, which is contained within another array. For this algorithm, let us assume that there is an array of size MAX contained within an array of size 5, MAX of course would be a declared constant and could be any size.

{ totals values contained in a two-dimension array } total := 0; FOR I := 1 TO 5 DO FOR J := 1 TO MAX DO total := total + data_store[I,J]; WRITELN('The total equals : ',total);

Now, how does this work? Well when the loop controlled by I is reached, I becomes equal to one, then the program moves on to the loop controlled by J, which also becomes equal to one. So with both I and J equal to one the program reaches the third line, where quite simply the variable total takes the value of itself plus the value contained in the array at location [1,1]. Now the program is still within the loop controlled by J, and will be until J reaches the value MAX, at which point I will still be equal to one and J will be equal to MAX. So the sum of all the values contained in the array from [1,1] to [1,MAX] will have been added to total. Now the program will return to the loop controlled by I. I will be incremented to two, and the program will move on to the loop controlled by J, at which point J will be restarted at 1, and the first value added to total will be the value contained in the array location [2,1]. Get it? The program is still within the loop controlled by J, and will be there until J again equals MAX at which point I will still be equal to two, and J will be equal to MAX, and all the values contained in the from [2,1] to [2,MAX] will have been added to total. This repetition will continue until I equals five and J equals max then both loops will be exited from and the program will print out the value of total.

The next little program demonstrates the coordinates of two-dimensional arrays by writing out coordinates.

Program 3

PROGRAM coordinate_write(OUTPUT); { this program demonstrates how coordinates can be accessed in a two-dimensioned array by using nested FOR loops } VAR X, Y : INTEGER; BEGIN FOR Y := 1 TO 5 DO BEGIN FOR X := 1 TO 10 DO WRITE('(',X:1,',',Y:1,')'); WRITELN; END; END. { program coordinate_write }

The output from this program is shown in figure



(1,1)(2,1)(3,1)(4,1)(5,1)(6,1)(7,1)(8,1)(9,1)(10,1) (1,2)(2,2)(3,2)(4,2)(5,2)(6,2)(7,2)(8,2)(9,2)(10,2) (1,3)(2,3)(3,3)(4,3)(5,3)(6,3)(7,3)(8,3)(9,3)(10,3) (1,4)(2,4)(3,4)(4,4)(5,4)(6,4)(7,4)(8,4)(9,4)(10,4) (1,5)(2,5)(3,5)(4,5)(5,5)(6,5)(7,5)(8,5)(9,5)(10,5)

Figure 3 - Two-dimensional arrays : Writing out the coordinates

A similar procedure occurs with arrays of more dimensions. A three dimensional array would be defined by:

TYPE CUBE_ARRAY = ARRAY[1..MAX,1..MAX,1..MAX] OF INTEGER; VAR data_store : CUBE_ARRAY;

To run through this particular data structure will require three coordinates, and triple nested FOR loop.

{ totals values in three_ dimensional array } total := 0; FOR I := 1 TO MAX DO FOR J := 1 TO MAX DO FOR K := 1 TO MAX DO total := total + data_store[I,J,K]; WRITELN('Total is : ',total);

Let's return to two-dimensional arrays. If you look at the array as having rows and columns, you might at some point wish to total up the rows and the columns individually. This next program does that. Consider a two-dimensional array of five by five. The fifth element in each row is to contain the total for that row, and the fifth element in each column is to contain the total for that column. You will notice that I have used and reused the loop counters I and J. This may cause you a little problem, but I will explain what makes this ok later on. In this next program, because we will be reading values into the 4 by 4 part of the array, it is only necessary to zero the elements of the 5th row and the 5th column.

Program 4

array}

PROGRAM row_column_total(INPUT,OUTPUT); { program totals up rows and columns of a two-dimensional array } CONST MAX = 5; TYPE TOTAL_ARRAY = ARRAY[1..MAX,1..MAX] OF INTEGER; VAR row, column : INTEGER; the_array : TOTAL_ARRAY; BEGIN FOR row := 1 TO MAX DO BEGIN the_array[row,MAX] := 0; { initialises 5th column } the_array[MAX,row] := 0; { initialises 5th row } END; FOR row := 1 TO 4 DO { reads the numbers } FOR column := 1 TO 4 DO { into the array } BEGIN WRITE('Enter an integer for '); WRITE('(',row,',',column,') : '); READLN(the_array[row,column]); { read integer directly into array } END; { without use of intemediate variable } FOR row := 1 TO 5 DO { write out contents of the array } BEGIN WRITELN('+---+---+---+---+---+'); FOR column := 1 TO 5 DO WRITE('|',the_array[row,column]:2,' '); WRITELN('|'); END; WRITELN('+---+---+---+---+---+'); FOR row := 1 TO 4 DO { totals the rows } FOR column := 1 TO 4 DO the_array[row,5] := the_array[row,5] + the_array[row,column]; FOR column := 1 TO 4 DO { totals the columns } FOR row := 1 TO 4 DO the_array[5,column] := the_array[5,column] + the_array[row,column]; WRITE('Press <enter> to see totals'); READLN; writeln; writeln('The totals of the rows and columns, are'); FOR row := 1 TO 5 DO { write out contents of the array } BEGIN WRITELN('+---+---+---+---+---+'); FOR column := 1 TO 5 DO WRITE('|',the_array[row,column]:2,' '); WRITELN('|'); END; WRITELN('+---+---+---+---+---+'); END. { program row_column_total }

The next piece of code worth looking a bit more closely at is

FOR row := 1 TO 4 DO { reads the numbers } FOR column := 1 TO 4 DO { into the array } BEGIN WRITE('Enter an integer for '); WRITE('(',row,',',column,') : '); READLN(the_array[row,column]); { read integer } END; { straight into the } { array without use of} { another variable }

It has the pair of FOR loops, that you know about. The line

READLN(the_array[row,column]);

is not as strange as it might seem. At each iteration of the loop the pair [row,column] will identify a different element in the array so that in each iteration the_array[row,column] is in fact a different element of the array. The line in effect is saying put the entered number into the variable the_array at the coordinates indicated by the pair [row,column].

We could have written this operation like this:

BEGIN WRITE('Enter an integer : '); READLN(a_number); the_array[row,column] := a_number; END;

This does the same thing, but because it uses another variable, this method uses more memory, and time, and as such it is not quite as good. It is not wrong; it is not bad. The first way is just better, but the second might be clearer to you. In programming the best way is always the way which you understand the most thoroughly, because it might be you who has to edit a program, and if you really don't understand how it works how can you do this properly? So in the program the four by four part of the array is loaded up with integers, and could look something like this,

+---+---+---+---+---+ | 1 | 2 | 3 | 4 | 0 | 1,5 +---+---+---+---+---+ | 5 | 6 | 7 | 8 | 0 | 2,5 +---+---+---+---+---+ | 9 |10 |11 |12 | 0 | 3,5 +---+---+---+---+---+ |13 |14 |15 |16 | 0 | 4,5 +---+---+---+---+---+ | 0 | 0 | 0 | 0 | 0 | 5,5 +---+---+---+---+---+ 5,1 5,2 5,3 5,4 5,5

Figure 4 - Two dimensional arrays : Array of integers

Now we need to figure out how to total each row and put it into the fifth element of that row. The fifth element of the row really has its own a separate identifier. This is the_array[row,MAX]. As row varies in the loop between one and four, MAX remains constant. So the total will be stored in the fifth element of the n'th row. In the first row we wish to perform the calculation

the_array[1,1] + the_array[1,2] + the_array[1,3] + the_array[1,4] ------------------- the_array[1,5] :=

Of course instead of the constant subscripts, we are going to use the loop counter variable row and column as in the code fragment below.

FOR row := 1 TO 4 DO { totals the rows } FOR column := 1 TO 4 DO the_array[row,MAX] := the_array[row,5] + the_array[column,row];

This piece of code adds together the first four elements of each row and puts the result into the fifth element. Next we want to total up the first four elements in each column and put the result into its fifth element. The operation we wish to perform on the first column is

the_array[1,1] + the_array[2,1] + the_array[3,1] + the_array[4,1] --------------------- the_array[5,1] :=

When totalling the rows the outside FOR loop was driven by the variable row. We now want to go vertically so it will be the second index which varies. This means that we will sweep across the array in a vertical mode for columns rather than the horizontal mode of rows. The code which does the column totalling is only subtly different from the row totalling code.

FOR column := 1 TO 4 DO { totals the columns } FOR row := 1 TO 4 DO the_array[5,column] := the_array[5,column] + the_array[row,column];

The code which writes out the array is copied from the program sort_array in chapter five adapted to work on a two-dimensional array. Well, earlier I promised to explain why it was ok to do the whole of program row_column _total using and reusing only two loop counter variables. This really comes down to a very simple concept. A looping structure is the piece of code where a variable is under control of the loop. Once the program leaves the loop, the loop no longer has any control over the variable used as a control variable. The variable is therefore available to be used again. The range of control of FOR, REPEAT - UNTIL and WHILE loops are begun with the reserved word, and ended with either a semi-colon in the case of single statement loops or the reserved word END. Such as in the examples below.

FOR I := 1 TO 5 DO WRITE('-');

this is an example of a single statement loop which ends at the semi-colon. This is exactly the same as

FOR I := 1 TO 5 DO WRITE('-');

adding a BEGIN - END pair extends the range of the loops control to the reserved word END.

FOR I := 1 TO 5 DO BEGIN WRITE('-'); END;

This fragment will work exactly the same as the other two above. The BEGIN - END pair only extends the range of the loop to allow you to put more statements under its control, such as you have seen in other programs. So once the program leaves the loop, we can then start another loop using the same control variables.

1.3. Statements! What are statements?

A statement is a single complete instruction, and statements are separated by semi-colons. A compound statement is two or more simple statements grouped together between a BEGIN and an END.

This is a statement.

answer := first_number + second_number;

And this is a compound statement.

BEGIN first_number := first_number + 1; second_number := second_number + first_number; third_number := third_number + first_number; END;

Date: 2016-03-03; view: 657


<== previous page | next page ==>
Multi-Dimensional Arrays | Enumerated types, as array subscripts
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)