TOPIC D: Multidimensional arrays
Contents :
Multidimensional arrays.
Exercises :
D1: Matrix Sum.
- Define two N by N matrices of integers: matrix A and B. Fill it
according to the following rules:
- A[i, j] = i + j and B[i, j] = i - j (with i, j from 1
to N)
- print the array A
- Sum both matrices into matrx C
- C[i, j] = A[i, j] + B[i, j]
- print also B & C
S1: Matrix Product.
- Create two 5*5 integer matrices A & B and initialise them:
- A[i, j] = (i * 2 + j) MOD 3 and B[i, j] = (i * 2 - j)
- Calculate the matrix product
- C = A X B
- or C[i, j] = A[i, 1] * B[1, j] + A[i, 2] *
B[2, j] + ... + A[i, 5] * B[5, j]
- What is the average value of the elements of the C-matrix?
- use a REAL variable to calculate it (don't forget the
conversion) (see next topic)
S2: Hidden Character.
Define an array Arr of 5 rows and 7 columns.
- Fill it: (copy this into your code)
Arr[1][1] := 2;
Arr[1][2] := -3;
Arr[1][3] := 5;
Arr[1][4] := 777;
Arr[1][5] := 89;
Arr[1][6] := -9;
Arr[1][7] := 44;
Arr[2][1] := 22;
Arr[2][2] := 30;
Arr[2][3] := -2;
Arr[2][4] := 43;
Arr[2][5] := 0;
Arr[2][6] := 10;
Arr[2][7] := 20;
Arr[3][1] := 30;
Arr[3][2] := 32;
Arr[3][3] := 322;
Arr[3][4] := 9;
Arr[3][5] := 56;
Arr[3][6] := 88;
Arr[3][7] := -888;
Arr[4][1] := 45;
Arr[4][2] := 8;
Arr[4][3] := 88;
Arr[4][4] := 89;
Arr[4][5] := 88;
Arr[4][6] := 82;
Arr[4][7] := 86;
Arr[5][1] := 98;
Arr[5][2] := 99;
Arr[5][3] := 99;
Arr[5][4] := 98;
Arr[5][5] := 96;
Arr[5][6] := -94;
Arr[5][7] := 92;
- Print the given array as follows:
- Print all the elements, first the rows, than the columns:
- Print a '0' if the number is a even number (divisible by 2),
print an 'I' otherwise.
- Start a newline for each new row
- Which character do you see?
S3: Chess Board.
Use TopicB D2 part1 as a start.
- Draw a chess board with 2 colours different from black.
S4: Matrix Figures.
Start with the graphics .
- Define two constants: N and SIZE, give both value 10.
- Define two 2-dimensional N by N arrays of integers, named A and
B.
- Fill the arrays according to this formula:
- Aij = i + 2 x j
- Bij = 3 x i - j
- This can become negative: so use integers for A and B, but
also for the index's!!
- Draw NxN rectangles with
- x = (2 x Aij + Bji - 5) x SIZE
- y = (2 x Aji + Bij - 5) x SIZE (Note the
order of i & j!!)
- SIZE: the size of the blocks
- let the colour change for every block you draw (invent a
formula)
S5: Bitmaps.
- Copy the code from . We defined bitmap as a two-dimensional array
of characters. In the colormap (one-dimensional arrays) each character
is mapped on a color.
- Put the bitmap on the screen. Use:
- rectangles of a certain size
- or the SetPixel(x,y,color)
procedure to color 1 pixel.
-
S6: Bewegende Circels
1. Gebruik de volgende 2-dimensionale array als
middelpunt-coordinaten om circels te tekenen van grootte SIZE:
CONST SIZE=10;
a[1][1] := 200;
a[1][2] := 300;
a[2][1] := 123;
a[2][2] := 456;
a[3][1] := 512;
a[3][2] := 500;
a[4][1] := 123;
a[4][2] := 50;
a[5][1] := 432;
a[5][2] := 300;
a[6][1] := 234;
a[6][2] := 510;
2. Laat de circels horizontaal naar rechts bewegen (zie voor
beweging topic B D2b)
X1: Check.
Define a 5 by 4 integer matrix and fill it (copy this code):
A[1][2] := 1;
A[2][3] := 1;
A[1][4] := 1;
A[2][1] := -1;
A[3][1] := 1;
A[1][2] := 1;
A[4][1] := 1;
A[4][2] := 1;
- Check if the matrix meets the following criteria:
- if i < 3: A[i, j] = A[(j*2)MOD 3 + 1, i] - A[i+2, j]
- if i > 3: A[i, j] = A[j, i-1] - A[i-2, j]
- with i from 1 to 5 and j from 1 to 4
- Print the indeces of the wrong elements
- Print the total number of errors
- Correct the wrong elements by giving them the calculated value
according to the formula.
- is the resulting matrix correct now?
- if not, correct the resulting matrix again and test it
again,...
- do this in a loop of maximal 100 iterations or until a
solution is found.
- is there a solution? A stable matrix where the formula is
correct for all elements.
X2: Rain (this one is a real challenge!!)
Let it rain! (See TopicB D2 for an
introduction to graphics)
- Drops (use a circle) are falling down with a certain velocity.
- Hint: Use an array of drops. Give them all a different initial
position.
- I want a 'random' rain.
- Hint: when a drop reaches the ground, let it start from the
top again, but from a random horizontal position.
- Use the formula of TopicA X1 to
generated 'random' numbers between 0 and 96 (which you can scale
yourself).
H1: Print.
- Print out the matrix of exercise S2, with the following layout:
[ 1, 2, 13]
[-12, -14, 0]
[ 0, 23, -20]
[ ...
]
T1: Output
- What is the output of the following program?
- try to understand the functionality of the program!
MODULE T1;
<* NOOPTIMIZE + *>
FROM IO IMPORT RdChar, WrChar, WrStr, RdStr,
WrLn, RdCard, WrCard, RdInt, WrInt; (*
import general procedures *)
CONST
ARRAY_SIZE = 7;
STRING_SIZE = 20;
NBR_V =
5; (* constants *)
VAR wordsArr:
ARRAY[1..ARRAY_SIZE],[1..STRING_SIZE] OF
CHAR;
(* variable-declarations *)
nbrArr: ARRAY[1..ARRAY_SIZE] OF
CARDINAL;
i, j, mn, mni, mx, mxi: CARDINAL;
BEGIN
WrLn;
wordsArr[1] := "Modula2";
wordsArr[2] := "programmerscourse";
wordsArr[3] := "students";
wordsArr[4] := "windows";
wordsArr[5] := "XDS";
wordsArr[6] := "what if?";
wordsArr[7] := "could be!";
FOR i := 1 TO ARRAY_SIZE DO
FOR j := 1 TO STRING_SIZE DO
IF (wordsArr[i][j] = 'a')
OR
(wordsArr[i][j] = 'e')
OR
(wordsArr[i][j] = 'i')
OR
(wordsArr[i][j] = 'o')
OR
(wordsArr[i][j] = 'u') THEN
INC(nbrArr[i]); (* INC = increment eg INC(a); is the same
as a := a + 1; *)
END;
END;
IF (i = 1) THEN
mn := nbrArr[i];
mni := i;
mx := nbrArr[i];
mxi := i;
ELSE
IF (nbrArr[i] < mn)
THEN
mn :=
nbrArr[i];
mni := i;
END;
IF (nbrArr[i] > mx)
THEN
mx :=
nbrArr[i];
mxi := i;
END;
END;
END;
WrStr("The m.... is "); WrCard(mn, 0); WrStr(", word:
"); WrStr(wordsArr[mni]);WrLn;
WrStr("The m.... is "); WrCard(mx, 0); WrStr(", word:
"); WrStr(wordsArr[mxi]);WrLn;
END T1.