Solutions TOPIC E: Reals & Booleans

Contents :

  Integer, + - * DIV MOD, IF, FOR, WHILE, RdInt, WrInt.

Exercises :


D1

Exercise
MODULE D1;

FROM Wimdows IMPORT Window, KillWindow, SetPixel, GetKey, SetFont, Text;
FROM Wimdows IMPORT _clrBLUE, _clrGREEN, _clrRED, _clrWHITE;
FROM IO IMPORT WrStr, WrChar, WrCard, WrLn, RdLn, RdChar, KeyPressed, WrFixReal, RdLnOnWr,RdKey;
FROM RealMath IMPORT sin, pi, cos;
FROM Lib IMPORT Delay;  (* Wait n milliseconds *)
 
CONST
  GRAPH_WIDTH = 360;
  GRAPH_HEIGHT = 360;
  SCALE = 50;

VAR
  sin_x : REAL;
  t, x,y : CARDINAL;
  c:CARDINAL;
BEGIN
     Window("Draw sin(x)", GRAPH_WIDTH, GRAPH_HEIGHT); 

     (* draw composition in given colour *)
    FOR t := 0 TO 360 DO
      x := t;
      sin_x := sin( VAL(REAL, x) * pi / 180.0); (* convert x to REAL & to radians *)
      sin_x := sin_x * VAL(REAL, SCALE) ; (* verschaling *)
      sin_x := sin_x + VAL(REAL, GRAPH_HEIGHT / 2) ; (* translatie *)
      y := VAL(CARDINAL, sin_x) ; (* convert sin_x to a CARDINAL value *)

      SetPixel(x, y, _clrWHITE);

      WrCard(x,1); WrStr(" "); WrCard(y,1); WrLn();
    END; (* FOR *)

  (* tell user to press a key to finish the program... *)
  SetFont(1, _clrRED);
  Text("press any key to finish the program", GRAPH_WIDTH/2-120, 10);
  (* ...and program will wait here until user presses a key *)
  c := GetKey();
  (* finally close the window *)
  KillWindow();

END D1.


S1

Exercise

   opl?    x1      x2
   TRUE    0.428   -0.430
   FALSE
   TRUE    -1.000  -1.000


S2

Exercise

S3

Exercise

X1

Exercise

H1

Exercise

H2

MODULE H2;
<* NOOPTIMIZE + *>
 FROM IO IMPORT WrStr, WrLn, WrInt, WrChar, WrCard, WrFixReal;

  CONST
    MAX_DOBBELSTEEN = 6;
    MAX_GANZENBORD = 63;

  VAR
    ganzenBord: ARRAY[1..MAX_GANZENBORD] OF INTEGER;
    d1, d2: ARRAY[1..12] OF INTEGER; (* de uitslagen van 2 dobbelstenen *)
    p: INTEGER; (* positie van P *)
    gemSprong: REAL;
BEGIN
  WrLn;

  (* het ganzenbord
     een positieve hindernis is een positief getal, een negatieve hindernis een negatief getal *)
  ganzenBord[1] := 0;  ganzenBord[2] := +1;  ganzenBord[3] := 0;  ganzenBord[4] := +3; ganzenBord[5] := 0;
  ganzenBord[6] := +1;  ganzenBord[7] := 0;  ganzenBord[8] := 0;  ganzenBord[9] := -3; ganzenBord[10] := +3;
  ganzenBord[11] := -2;  ganzenBord[12] := -1;  ganzenBord[13] := 0;  ganzenBord[14] := +4; ganzenBord[15] := 0;
  ganzenBord[16] := +2;  ganzenBord[17] := 0;  ganzenBord[18] := 0;  ganzenBord[19] := 0; ganzenBord[20] := 0;
  ganzenBord[21] := 0;  ganzenBord[22] := 0;  ganzenBord[23] := 0;  ganzenBord[24] := 0; ganzenBord[25] := +3;
  ganzenBord[26] := 0;  ganzenBord[27] := +6;  ganzenBord[28] := 0;  ganzenBord[29] := -1; ganzenBord[30] := 0;
  ganzenBord[31] := -3;  ganzenBord[32] := 0;  ganzenBord[33] := 0;  ganzenBord[34] := 0; ganzenBord[35] := 0;
  ganzenBord[36] := 0;  ganzenBord[37] := 0;  ganzenBord[38] := 0;  ganzenBord[39] := +2; ganzenBord[40] := +4;
  ganzenBord[41] := +5;  ganzenBord[42] := 0;  ganzenBord[43] := 0;  ganzenBord[44] := 0; ganzenBord[45] := -1;
  ganzenBord[46] := -7;  ganzenBord[47] := +9;  ganzenBord[48] := 0;  ganzenBord[49] := +5; ganzenBord[50] := 0;
  ganzenBord[51] := 0;  ganzenBord[52] := 0;  ganzenBord[53] := 0;  ganzenBord[54] := 0; ganzenBord[55] := 0;
  ganzenBord[56] := +2;  ganzenBord[57] := -3;  ganzenBord[58] := 0;  ganzenBord[59] := -1; ganzenBord[60] := 0;
  ganzenBord[61] := 0;  ganzenBord[62] := 0;  ganzenBord[63] := 0;

  (* de uitslagen van de 2 dobbelstenen *)
  d1[1] := 3; d2[1] := 1;
  d1[2] := 2; d2[2] := 3;
  d1[3] := 3; d2[3] := 4;
  d1[4] := 1; d2[4] := 6;
  d1[5] := 6; d2[5] := 6;
  d1[6] := 2; d2[6] := 2;
  d1[7] := 6; d2[7] := 6;
  d1[8] := 2; d2[8] := 3;
  d1[9] := 5; d2[9] := 3;
  d1[10] := 2; d2[10] := 5;
  d1[11] := 2; d2[11] := 5;
  d1[12] := 3; d2[12] := 1;

  (* laat zien hoe P het ganzenbord afloopt *)

  p := 0; (* speler P begint op positie 0 en moet voorbij de 62 geraken *)
 

  (* in hoeveel beurten is hij voorbij de finish geraakt? *)
 

  (* gemiddelde sprong per beurt *)
  WrStr("Speler P springt gemiddeld ");WrFixReal(gemSprong, 3, 0);WrStr(" vakjes per beurt vooruit.");WrLn;
END H2.
Exercise

Oplossing:
Worp 1 => vak 7
Worp 2 => vak 7
Worp 3 => vak 18
Worp 4 => vak 28
Worp 5 => vak 44
Worp 6 => vak 48
Worp 7 => vak 60
Worp 8 => vak 65
Na 8 worpen is P tot voorbij de finish geraakt met een gemiddelde sprong van 8.12.


T1

Exercise