Solutions TOPIC B: Arrays

Contents :

   ARRAY of INTEGERS,  CONST.

Exercises :


D1

Exercise

MODULE D1Part1;

    FROM IO IMPORT WrStr, WrLn, RdCard, WrCard, RdInt, WrInt;     (* import general procedures *)

    VAR
      intArray: ARRAY[1..10] OF INTEGER;            (* variable-declarations *)
      i:CARDINAL;

BEGIN
    (* Fill array *)
    FOR i := 1 TO 10 DO
      intArray[i] := 1;
    END;

    (* Print array *)
    FOR i := 1 TO 10 DO
      WrStr("Element "); WrCard(i, 0); WrStr(" of array is: "); WrInt(intArray[i],0); WrLn;
    END;

END D1Part1.



MODULE D1Part2;

    FROM IO IMPORT WrStr, WrLn, RdCard, WrCard, RdInt, WrInt;     (* import general procedures *)

    CONST ARRAY_SIZE = 20;       (* constants *)  
    VAR

      intArray: ARRAY[1..ARRAY_SIZE] OF INTEGER;            (* variable-declarations *)
      i:CARDINAL;

BEGIN
    WrLn;
    (* Fill array *)
    FOR i := 1 TO ARRAY_SIZE DO
      intArray[i] := i;
    END;

    (* Print array *)
    FOR i := 1 TO ARRAY_SIZE DO
      WrStr("Element "); WrCard(i, 0); WrStr(" of array is: "); WrInt(intArray[i],0); WrLn;
    END;

END D1Part2.


D2

Exercise

MODULE D2a;

<* NOOPTIMIZE + *>  (* For the Debugger *)
  FROM IO IMPORT WrStr, WrLn, RdKey, WrCard, RdLn;

  FROM Wimdows IMPORT GetKey;
  FROM Wimdows IMPORT SetFont, Text;
  FROM Wimdows IMPORT Window, KillWindow, Line,  Rectangle, Circle, Disc;
  FROM Wimdows IMPORT _clrBLACK, _clrBLUE, _clrGREEN, _clrCYAN, _clrRED, _clrMAGENTA;
  FROM Wimdows IMPORT _clrBROWN, _clrWHITE, _clrGRAY, _clrLIGHTBLUE, _clrLIGHTGREEN;
  FROM Wimdows IMPORT _clrLIGHTCYAN, _clrLIGHTRED, _clrLIGHTMAGENTA, _clrLIGHTYELLOW, _clrBRIGHTWHITE;

                 (* deze 16 kleuren zijn constantes gaande van 0 tot 15 *)

  VAR
   x:CARDINAL;

BEGIN

  (* Initialise graphics *)
  Window("titel", 500, 500);
  (* coordinatenstelsel: (0,0) is in linkerbovenhoek *)

  Rectangle(200, 100, 300, 200, _clrCYAN, TRUE);
   (*       x, y van linkerbovenhoekpunt
           x, y van rechterbenedenhoekpunt
                   kleur, vullen?  *)

  Disc(300, 300, 50, _clrGRAY);
   (* x, y van middelpunt, straal, kleur *)

  Circle(400, 400, 20, _clrLIGHTBLUE);
   (* x, y van middelpunt, straal, kleur *)

  (* tell user to press a key to finish the program... *)
  SetFont(1, _clrRED);
  Text("press any key to finish the program", 135, 10);

  (* ...and program will wait here until user presses a key *)
  x := GetKey();

  (* finally close the window *)
  KillWindow();

END D2a.


MODULE D2b;
<* NOOPTIMIZE + *>  (* For the Debugger *)
  FROM IO IMPORT WrStr, WrLn, RdKey, WrCard, RdLn;
  FROM Lib IMPORT Delay; (* Delay: let the program wait a little time *)

  FROM Wimdows IMPORT GetKey;
  FROM Wimdows IMPORT SetFont, Text;
  FROM Wimdows IMPORT Window, KillWindow, Line,  Rectangle, Circle, Disc;
  FROM Wimdows IMPORT _clrBLACK, _clrBLUE, _clrGREEN, _clrCYAN, _clrRED, _clrMAGENTA;
  FROM Wimdows IMPORT _clrBROWN, _clrWHITE, _clrGRAY, _clrLIGHTBLUE, _clrLIGHTGREEN;
  FROM Wimdows IMPORT _clrLIGHTCYAN, _clrLIGHTRED, _clrLIGHTMAGENTA, _clrLIGHTYELLOW, _clrBRIGHTWHITE;
                 (* deze 16 kleuren zijn constantes gaande van 0 tot 15 *)

       

  CONST  DELAY_TIME = 1000;

  VAR

   x:CARDINAL;

BEGIN

  (* Initialise graphics *)
  Window("titel", 500, 500);
  (* coordinatenstelsel: (0,0) is in linkerbovenhoek *)

   Rectangle(200, 100, 300, 200, _clrCYAN, TRUE);
   Delay(DELAY_TIME);

   Rectangle(200, 100, 300, 200, _clrBLACK, TRUE);
   Disc(300, 300, 50, _clrGRAY);
   Delay(DELAY_TIME);

   Disc(300, 300, 50, _clrBLACK);
   Circle(400, 400, 20, _clrLIGHTBLUE);
   Delay(DELAY_TIME);

   Circle(400, 400, 20, _clrBLACK);

  (* tell user to press a key to finish the program... *)
  SetFont(1, _clrRED);
  Text("press any key to finish the program", 135, 10);

  (* ...and program will wait here until user presses a key *)
  x := GetKey();

  (* finally close the window *)
  KillWindow();

END D2b.


D3

Exercise
 


S2

Exercise

 MODULE S2;
<* NOOPTIMIZE + *>  (* For the Debugger *)

  FROM IO IMPORT WrStr, WrLn, RdKey, WrCard, WrInt, RdLn;

  CONST ARRAY_SIZE = 10;

  VAR
    i: CARDINAL;
    f: INTEGER;
    y_values: ARRAY[1..ARRAY_SIZE] OF INTEGER; (* definition of the array *)
BEGIN

   f := 1;
  (* Fill Array  HIER NIET AANKOMEN! *)
  FOR i:= 1 TO ARRAY_SIZE DO
    f :=  (f + (f)*4/15 + 1) MOD 40;
(*   f := f + (20-f) * ABS(f) / 7;  *) (* another function *)
    y_values[i] := f;
  END;

  (* ZET JE CODE HIER: is de functie monotoon stijgend ??*)


(* Print array *)
  FOR i:= 1 TO ARRAY_SIZE DO
    WrStr("The ");WrCard(i, 0);WrStr("th element =");WrInt(y_values[i], 0);WrLn;
  END;
  WrLn;

 
END S2.



S4

Exercise

MODULE S4;
<* NOOPTIMIZE + *>  (* For the Debugger *)
  FROM IO IMPORT WrStr, WrLn, RdKey, WrCard, RdLn;
  FROM Lib IMPORT Delay; (* Delay: let the program wait a little time *)

  FROM Wimdows IMPORT GetKey;
  FROM Wimdows IMPORT SetFont, Text;
  FROM Wimdows IMPORT Window, KillWindow, Line,  Rectangle, Circle, Disc;
  FROM Wimdows IMPORT _clrBLACK, _clrBLUE, _clrGREEN, _clrCYAN, _clrRED, _clrMAGENTA;
  FROM Wimdows IMPORT _clrBROWN, _clrWHITE, _clrGRAY, _clrLIGHTBLUE, _clrLIGHTGREEN;
  FROM Wimdows IMPORT _clrLIGHTCYAN, _clrLIGHTRED, _clrLIGHTMAGENTA, _clrLIGHTYELLOW, _clrBRIGHTWHITE;
                 (* deze 16 kleuren zijn constantes gaande van 0 tot 15 *)

  CONST ARRAY_SIZE = 20;
        SIZE = 7;

  VAR
    i: CARDINAL;
    f, x, y: INTEGER;
    y_values: ARRAY[1..ARRAY_SIZE] OF INTEGER; (* definition of the array *)
    c: CARDINAL;

BEGIN

  (* Initialise graphics *)
  Window("titel", 500, 500);

  f := 1;

  (* Fill Array *)
  FOR i:= 1 TO ARRAY_SIZE DO
    f :=  (f + (f)*4/15 + 1) MOD 40;
(*    f := f + (20-f) * ABS(f) / 7;*)  (* another function *)
    y_values[i] := f;
  END;

  (* Plot graphics here *)



  (* tell user to press a key to finish the program... *)
  SetFont(1, _clrRED);
  Text("press any key to finish the program", 135, 10);

  (* ...and program will wait here until user presses a key *)
  c := GetKey();

  (* finally close the window *)
  KillWindow();

END S4.


S6

Exercise

H1


X1

Exercise

 

T1

Exercise