Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Multi-Dimensional Arrays

Multi-dimensional arrays are a little more tricky that single-dimension arrays, but they are not much more. Can you imagine two single dimension arrays, side-by-side? The contents of the first array holding the names of countries and the second, the names of their capital cities. The simplest way of explaining arrays I find is to think of cartesian coordinates. Remember (x,y) from drawing graphs? The x is along the way, and the y is up or down. With the first program in this chapter, I will be tying these two arrays together by declaring them together and thus declaring into a two-dimensional array. You will remember that to declare a one dimensional array uses code like,

CONST MAX = 5; TYPE NAMES = STRING[15]; NAME_ARRAY = ARRAY[1..MAX] OF NAMES;

This will make available a five element array with each element able to contain a string of up to fifteen characters. With this declaration, we have declared an array with only x coordinates. Now to declare a two-dimensional array, with both x and y coordinates, capable of holding the names of countries and their capital cities, needs a piece of code like,

CONST COLUMNS = 2; { x coordinates } ROWS = 5; { y coordinates } TYPE NAMES = STRING[15]; AN_ARRAY = ARRAY[1..COLUMNS,1..ROWS] OF NAMES;

Can you see the part of the declaration which causes the two-dimensions? Try to imagine a grid, two-by-five. For that is about the best analogy for this kind of data structure. You could of course use constants called X and Y instead, rather that COLUMNS and ROWS. In the program below the data is loaded into the array inside the program by assignment. Position (1,1) is loaded with the country 'Britain' and position (2,1) is loaded with the city 'London' and so on until position (2,5) is loaded with the city 'Oslo'. If you put this down on paper it would look something like this.

Figure 1 - Two dimensional arrays : Countries and Capital

So if I asked you the question, What is the capital of the country in position (1,1)? You would say, Well (1,1), that's Britain so the answer is London. I would compare your answer with the contents of position (2,1) and if they are exactly the same I would tell you that you are correct. This is in fact all that the first program does.

Program 1

PROGRAM capitals(INPUT,OUTPUT); { this program asks the user to enter either the capital of the named country, or the string 'pass' which will cause the program to move on to the next question } CONST COLUMNS = 2; { x coordinates } ROWS = 5; { y coordinates } TYPE NAMES = STRING[15]; AN_ARRAY = ARRAY[1..COLUMNS,1..ROWS] OF NAMES; VAR country : AN_ARRAY; capital : NAMES; I : INTEGER; correct : BOOLEAN; BEGIN country[1,1] := 'Britain'; country[2,1] := 'London'; country[1,2] := 'France'; country[2,2] := 'Paris'; country[1,3] := 'Spain'; country[2,3] := 'Madrid'; country[1,4] := 'Ireland'; country[2,4] := 'Dublin'; country[1,5] := 'Norway'; country[2,5] := 'Oslo'; WRITELN('Enter an answer or pass '); WRITELN(' Press enter to start'); READLN; FOR I := 1 TO 5 DO BEGIN REPEAT WRITE('What is the capital of ',country[1,I],' '); READLN(capital); correct := country[2,I] = capital; IF (capital = 'pass') THEN WRITELN('It is ',country[2,I]); UNTIL correct OR (capital = 'pass'); END; END. { program capitals }

The next program is based on the game called Battleships. This game is traditionally played on squared paper. In that game two players would draw out a fleet on a grid, with each vessel taking up so many squares, then they would take it in turn to call out coordinates, the other player would then reply whether there was a hit or a miss until one player had all their occupied boxes crossed off. In this program a Multi-dimensional array can be drawn on paper as below.



Figure 2 - Two dimensional arrays : Battleships

With the subscripts of arrays it is not necessary to stick to using numbers. All that is required is that the subscripts are and ordinal types. I have used subranges of the ordinal types CHAR and INTEGER. Therefor the submarine occupies the boxes (G,2), (H,2) and (I,2) not (2,G), (2,H) and (2,I). So now we could declare a two-dimensional array, MAX by MAX of type INTEGER.

CONST MAX = 10; TYPE INTEGER_ARRAY = ARRAY[1..MAX,1..MAX] OF INTEGER;

So now we have a ten by ten two-dimensional array set up. I have used in this example subranges of the ordinal type INTEGER for the subscripts, but for arrays any of the ordinal types can be used. You can even use your own enumerated types for subscripts as we shall see later in this chapter. For the program below I have decided to use subranges of both type INTEGER and CHAR.

TYPE CONDITION = STRING[10]; SEA_CHART = ARRAY['A'..'J',1..10] OF CONDITION;

This is how the above diagram is represented by code. You can see how 'A'..'J' corresponds to the subscripts from 'A' to 'J' and 1..10 corresponds to the subscripts from 1 to 10. The reason that the submarine occupies the boxes (G,2), (H,2) and (I,2) not (2,G), (2,H) and (2,I), is because of the fact that I have declared the dimensions in that order, 'A'..'J' first, then 1..10 so that any element of the array is reached by a letter then a number.

Program 2

PROGRAM battleships(INPUT,OUTPUT); { two-dimensional array demonstration program based on the game battleships } TYPE CONDITION = STRING[10]; SEA_CHART = ARRAY['A'..'J',1..10] OF CONDITION; VAR battle_zone : SEA_CHART; second, row : CHAR; first, hits, column : INTEGER; BEGIN hits := 0; FOR column := 'A' TO 'J' DO { FOR column 'A' TO column 'J' DO } FOR row := 1 TO 10 DO { FOR row 1 TO row 10 DO } battle_zone[row,column] := 'empty'; { initialise hit coordinates } battle_zone['G',2] := 'occupied'; { submarine } battle_zone['H',2] := 'occupied'; battle_zone['J',2] := 'occupied'; battle_zone['B',3] := 'occupied'; { destroyer } battle_zone['C',3] := 'occupied'; battle_zone['D',3] := 'occupied'; battle_zone['E',3] := 'occupied'; battle_zone['D',5] := 'occupied'; { battleship } battle_zone['E',5] := 'occupied'; battle_zone['F',5] := 'occupied'; battle_zone['G',5] := 'occupied'; battle_zone['H',5] := 'occupied'; battle_zone['B',7] := 'occupied'; { destroyer } battle_zone['C',7] := 'occupied'; battle_zone['D',7] := 'occupied'; battle_zone['E',7] := 'occupied'; REPEAT WRITE('Please enter coordinates : '); READLN(first,second); IF battle_zone[first,second] = 'occupied' THEN BEGIN WRITELN('A hit'); battle_zone[first,second] := 'destroyed'; hits := hits + 1; END ELSE IF battle_zone[first,second] = 'destroyed' THEN WRITELN('Already destroyed') ELSE WRITELN('A miss'); UNTIL hits = 16; WRITELN('Game Over'); END. { program battleships }

In the body of the program, the first step is to initialise each element of the array with the string 'empty'. This is done with two nested FOR loops. With multi-dimensional arrays a FOR loop is required for each dimension, to drive the search or insert algorithms. If you look back to the last chapter, you will notice that the searches only required one FOR loop.


Date: 2016-03-03; view: 672


<== previous page | next page ==>
The binary chop search algorithm | Moving though multi-dimensioned arrays
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)