TOPIC D: Multidimensional arrays

Contents :

  Multidimensional arrays.

Exercises :


D1: Matrix Sum.

  1. Define two N by N matrices of integers: matrix A and B. Fill it according to the following rules:
  2. Sum both matrices into matrx C

S1: Matrix Product.


S2: Hidden Character.

  • Define an array Arr of 5 rows and 7 columns.

  • S3: Chess Board.

    Use TopicB D2 part1 as a start.

    S4: Matrix Figures.

    Start with the graphics  .

    S5: Bitmaps.

    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;
    1. Check if the matrix meets the following criteria:
    2. Correct the wrong elements by giving them the calculated value according to the formula.

    X2: Rain (this one is a real challenge!!)

    Let it rain! (See TopicB D2 for an introduction to graphics)


    H1: Print.


    T1: Output

    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.