You are on page 1of 5

1

MULTIDIMENSIONAL ARRAYS The real problem is not to know whether machines think, but whether if men think "- B.F. Skinner The question of whether a computer can think is no more interesting than that of whether a submarine can swim "- Edgar w. Dijkstra 1. WHY SEVERAL DIMENSIONS? Only it wasn't enough already widely to our happiness, you ask? Of course, I will answer, but you will see that with two (and more) it's downright nirvana. Take the case of the modeling of a set of ladies, and moving the pawns on the Board. I recall that a pawn that is on a white box can move (to simplify) on four adjacent white squares. With the tools we have discussed so far, the simplest would obviously model the chequered flag in the form of a array. Each box is a location of the array, which contains, for example, 0 if it is empty, and 1 if there is a pawn. Is attributed as indices in boxes numbers 1 to 8 for the first row, 9 to 16 for the second row, and so on up to 64. Arrived at this point, fine flies of the genus Cyprien l write me for pointing out that a checkerboard, it has 100 boxes and 64, and that between the checkerboard and the chess boards, I myself am happily tangled pedals. At these fine flies, I will do a double response of prof: 1. It was to see if you follow. 2. Prof decides against all evidence that the diamonds are 64 squares, if the prof who is right and the evidence which is wrong. Break. Back to. A pawn placed in box i, in other words the value 1 in boxes (i), can move to the contiguous squares diagonally. This will force us to small intellectual acrobatics: the box located just above the box i with index i-8, the valid boxes are those of index i-7, i-9. Similarly, the checkbox just below with the index i + 8, valid cases are those of index i + 7, i + 9. Of course, one can make a program like this, but the least we can say is that this does not facilitate the clarity of the algorithm. It would be obviously simpler to model a checkerboard by... a checkerboard! 2 TWO DIMENSIONAL ARRAYS It offers us the possibility to declare arrays in which values are not chased by one, but by two coordinates. Such a array expresses thus: Array Cases(7,_7) digital

By bushemmy@minaffet.gov.rw

This means: reserve me a space of memory for 8 x 8 integers, and when I will need one of these values, I reprerai by two indices (such as the naval battle, or Excel, the only difference being that the coordinates are not used letters, just numbers). For our Queens problem, things are seriously thin. The box containing the pawn is now boxes (i, j). And the four available boxes are boxes (i-1, j-1), boxes (i - 1, j + 1), boxes (i + 1, j-1) and boxes (i + 1, j + 1). CRITICAL NOTE: There is no qualitative difference between a (i, j) two-dimensional array and an array to a dimension (i * j). And checkers just been mentioned, any problems that may be modeled in a way can also be modeled on the other. Simply, one or other of these techniques more spontaneously matches this or that problem, and therefore facilitates (or complicated, if we chose the wrong option) writing and readability of the algorithm. Another note: a classic question about two-dimensional arrays is of whether the first clue is the lines or the second columns, or vice versa. I will not answer not to this question not because I decided to sulk, but because it makes no sense. 'Lines' and 'Columns' are graphic, visual, concepts that apply to objects in the real world. The indices of the arrays are just logical coordinates, pointing to memory addresses vive. If this does not convince you, consider a classic naval battle game: letters should refer to rows and columns figures? No importance! Each player can even choose a different convention, no importance! The main thing is that once a chosen convention, a player retains the same throughout the game, of course.
EXERCISES

Exercise 1 Write an algorithm filling an array of 6 on 13, with zeros.

Exercise 2 What result will produce this algorithm? Array X(1,_2) as integer Variables i, j, val as integer Beginning Val 1 For i 0 to 1 For j 0-2 X (i, j) Val Val Val + 1 Next j i Next

By bushemmy@minaffet.gov.rw

For i 0 to 1 For j 0-2 Write X (i, j) Next j i Next End

Exercise 3 What result will produce this algorithm? Array X(1,_2) as integer Variables i, j, val as integer Beginning Val 1 For i 0 to 1 For j 0-2 X (i, j) Val Val Val + 1 Next j i Next For j 0-2 For i 0 to 1 Write X (i, j) i Next Next j End

Exercise 4 What result will produce this algorithm? Array T(3,_1) as integer Variables k, m as integer Beginning For k 0-3 For m 0 to 1
By bushemmy@minaffet.gov.rw

T (k, m) k + m m Next k Next For k 0-3 For m 0 to 1 Write T (k, m) m Next k Next End

Exercise 5 Same questions, by replacing the line: T (k, m) k + m by T (k, m) 2 ** k + (m + 1) then by: T (k, m) (k + 1) + 4 ** m

Exercise 6 Either a T (12, 8) two-dimensional previously filled array of numeric values. Write an algorithm that finds the highest value in this array.

Exercise 7 Write a very simplified algorithm of checkers. The computer prompts the user in which box is his pawn (which line, column). It sets up an entry control to verify the validity of the entered values. Then, it asks the user what movement they want to perform: 0 (top-left), 1 (top-right), 2 (lower-left), 3 (lower-right). If the movement is not possible (i.e. out of the checkerboard), reported to the user and it stops there. Otherwise, it moves the pawn and it displays result Checkerspot, showing an "O" for an empty box and a 'X' for the case where the pawn.
By bushemmy@minaffet.gov.rw

3 N-DIMENSIONAL ARRAYS If you have understood the principle of arrays in two dimensions, on the merits, there is no problem to pass to the handling of arrays at three, four, or why not nine dimensions. It is exactly the same thing. If I declare an array (2, 4, 3, 3) Titi, it is a memory space containing 3 x 5 x 4 x 4 = 240 values. Each value is marked by four coordinates. The main obstacle in the systematic use of these more than three dimensional arrays is that the programmer, when he designed his algorithm, like to the small scribbles, filthy drawings, imagine the loops in his head, etc. Gold, it is easy to imagine concretely to a onedimensional array, so this remains feasible for two dimensions, as it becomes the prerogative of a minority for the three dimensional arrays (I don't do unfortunately not part) and out of reach of any mortal beyond. It's like that, the human mind has trouble to represent things in space, and Cree grace as soon as he jumps into hyperspace (Yes, that is how it's called beyond three dimensions). So, for convenience only, the arrays with more than three dimensions are rarely used by non-mathematicians programmers (because the mathematicians, because of their training, have a nasty propensity to handle spaces in n dimensions as that channel, but these are well alone and let them in their corner, it's not people like us).

By bushemmy@minaffet.gov.rw

You might also like