Solutions TOPIC A: Basics

Contents :

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

Exercises :


D0

MODULE HelloWorld;

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

BEGIN
    WrStr("Hello World");
    WrLn;
   

END HelloWorld.


MODULE FirstVars;

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

    VAR a, b, x : INTEGER;

BEGIN
   
    a := 2;
    b := 10;

    x := (a + b) / 2;

    WrStr("The Average is:");
    WrLn;
    WrInt(x, 0);
    WrLn;

END FirstVars.


MODULE FirstInput;

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

    VAR a, b, x : INTEGER;

BEGIN
    WrStr("Give the first number");
    WrLn;  
    a := RdInt();

    WrStr("Give the second number"); 
    WrLn;
    b := RdInt();
    x := (a + b) / 2;

    WrStr("The Average is:");
    WrLn;
    WrInt(x, 0);
    WrLn;

END FirstInput.


D1

Exercise

MODULE D1part1;

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

    VAR x:CARDINAL;            (* variable-declarations *)

BEGIN
    WrStr("Please give me a number");
    WrLn;
    x := RdCard();

    IF ((x MOD 7) = 0) THEN
        WrCard(x, 0);
        WrStr(" is divisible by 7");
    ELSE
        WrCard(x, 0);
        WrStr(" is not divisible by 7");
    END;
    WrLn;

END D1part1.


 

MODULE D1part2;

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

    VAR x:CARDINAL;            (* variable-declarations *)

BEGIN
      WrLn;

      WrStr("Please give me a number");
      WrLn;
      x := RdCard();

      IF (x <= 10000) THEN
        WrStr("x must be greater than 10000!");
        WrLn;
      ELSE
        IF ((x MOD 7) = 0) THEN
          WrCard(x, 0);
          WrStr(" is divisible by 7");
        ELSE
          WrCard(x, 0);
          WrStr(" is not divisible by 7");
        END;
        WrLn;
      END;

END D1part2.


MODULE D1part3;

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

    VAR x, i:CARDINAL;            (* variable-declarations *)

BEGIN
    WrLn;

    FOR i := 1 TO 5 DO
      WrStr("Iteration: ");
      WrCard(i,1);
      WrLn;

      WrStr("Please give me a number");
      WrLn;
      x := RdCard();

      IF (x <= 10000) THEN
        WrStr("x must be greater than 10000!");
        WrLn;
      ELSE
        IF ((x MOD 7) = 0) THEN
          WrCard(x, 0);
          WrStr(" is divisible by 7");
        ELSE
          WrCard(x, 0);
          WrStr(" is not divisible by 7");
        END;
        WrLn;
      END;
    END;

END D1part3.  


MODULE D1part4;

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

    VAR x, i:CARDINAL;            (* variable-declarations *)

BEGIN
    WrLn;

    i := 0;
    REPEAT
      i := i + 1;
      WrStr("Iteration: ");
      WrCard(i,1);
      WrLn;

      WrStr("Please give me a number greater than 10000 and divisible by 7:");
      x := RdCard();

      IF (x <= 10000) THEN
        WrStr("x must be greater than 10000!");
        WrLn;
      ELSE
        IF ((x MOD 7) = 0) THEN
          WrCard(x, 0);
          WrStr(" is divisible by 7");
        ELSE
          WrCard(x, 0);
          WrStr(" is not divisible by 7: try again");
        END;
        WrLn;
      END;
    UNTIL ((x > 10000) AND ((x MOD 7) = 0));

    IF (i < 7) THEN
      WrStr("Congratulations: you've found one after ");
      WrCard(i,1);
      WrStr(" trials!");
      WrLn;
    ELSE
      WrStr("You've only found one after ");
      WrCard(i,1);
      WrStr(" trials!??");
      WrLn;
    END;

END D1part4.  


MODULE D1part5;

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

    VAR x, i, divisibleBy:CARDINAL;            (* variable-declarations *)

BEGIN
    WrLn;

    WrStr("Please give me a number: ");
    divisibleBy := RdCard();
    WrLn;

    i := 0;
    REPEAT
      i := i + 1;
      WrStr("Iteration: ");
      WrCard(i,1);
      WrLn;

      WrStr("Please give me a number greater than 10000 and divisible by ");
      WrCard(divisibleBy,1);
      WrStr(": ");
      x := RdCard();

      IF (x <= 10000) THEN
        WrStr("x must be greater than 10000!");
        WrLn;
      ELSE
        IF ((x MOD divisibleBy) = 0) THEN
          WrCard(x, 0);
          WrStr(" is divisible by ");
          WrCard(divisibleBy,1);
        ELSE
          WrCard(x, 0);
          WrStr(" is not divisible by ");
          WrCard(divisibleBy,1);
          WrStr(": try again");
        END;
        WrLn;
      END;
    UNTIL ((x > 10000) AND ((x MOD divisibleBy) = 0));

    IF (i < divisibleBy) THEN
      WrStr("Congratulations: you've found one after ");
      WrCard(i,1);
      WrStr(" trials!");
      WrLn;
    ELSE
      WrStr("You've only found one after ");
      WrCard(i,1);
      WrStr(" trials!??");
      WrLn;
    END;

END D1part5.  


MODULE D1part6;

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

    VAR x, i, divisibleBy, greaterThan:CARDINAL;            (* variable-declarations *)

BEGIN
    WrLn;

    WrStr("Please give me a number: ");
    divisibleBy := RdCard();
    WrLn;
    WrStr("Please give me a second number: ");
    greaterThan := RdCard();
    WrLn;

    i := 0;
    REPEAT
      i := i + 1;
      WrStr("Iteration: ");
      WrCard(i,1);
      WrLn;

      WrStr("Please give me a number greater than ");
      WrCard(greaterThan,1);
      WrStr(" and divisible by ");
      WrCard(divisibleBy,1);
      WrStr(": ");
      x := RdCard();

      IF (x <= greaterThan) THEN
        WrStr("x must be greater than ");
        WrCard(greaterThan,1);
        WrStr("!");
        WrLn;
      ELSE
        IF ((x MOD divisibleBy) = 0) THEN
          WrCard(x, 0);
          WrStr(" is divisible by ");
          WrCard(divisibleBy,1);
        ELSE
          WrCard(x, 0);
          WrStr(" is not divisible by ");
          WrCard(divisibleBy,1);
          WrStr(": try again");
        END;
        WrLn;
      END;
    UNTIL ((x > greaterThan) AND ((x MOD divisibleBy) = 0));

    IF (i < divisibleBy) THEN
      WrStr("Congratulations: you've found one after ");
      WrCard(i,1);
      WrStr(" trials!");
      WrLn;
    ELSE
      WrStr("You've only found one after ");
      WrCard(i,1);
      WrStr(" trials!??");
      WrLn;
    END;

END D1part6.


D2

Exercise
MODULE Demo2;

FROM Wimdows IMPORT Window, KillWindow, Text, SetFont, GetKey, Rectangle, Circle, Line, 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;

FROM IO IMPORT WrStr, WrChar, WrLn, RdLn, RdKey, RdCard, RdChar, KeyPressed;

CONST
  GRAPH_WIDTH = 500;  (* constantes die we gebruiken om de schermgrootte te definieren *)
  GRAPH_HEIGHT = 500;

VAR
  c, x, colour : CARDINAL; (* de kleur is een geheel getal *)

BEGIN
  (* Initialisatie van de graphics *)
  Window("titel", GRAPH_WIDTH, GRAPH_HEIGHT);

  (* Nu kan je figuren tekenen ...  *)

 (* Vraag de gebruiker voor een kleur *)
  WrStr("Geef kleur r, g of b (Rood, Groen of Blauw) : ");
  c := GetKey();  (* geeft een code terug afhankelijk van de ingedrukte toets *)

  IF c = ORD('R') THEN  (* we bepalen de kleur *)
    colour := _clrRED;
  ELSIF c = ORD('G') THEN
    colour := _clrGREEN;
  ELSIF c = ORD('B') THEN
    colour := _clrBLUE;
  ELSE
      WrStr("Ongeldige keuze (niet r, g of b)"); WrLn();
      WrStr("We tekenen met de kleur wit "); WrLn();
      colour := _clrWHITE;
  END; (* IF *)

  (* Teken de compositie met de gegeven kleur *)

  Circle(0,0,60,colour);
    (* circel met middelpunt (x, y), straal en kleur *)

  Line(0,0,200,200,colour);
    (* lijn van (x1, y1) tot (x2, y2) in een  kleur *)

  Rectangle(200, 200, 300, 400, colour, TRUE);
    (* rechthoek met linkerbovenhoek (x1, y1), rechterbenedenhoek (x2, y2),
     kleur en opgevuld of niet (TRUE/FALSE) *)



  (* einde van het programma *)
  (* vraag de gebruiker een toets te drukken... *)
  SetFont(1, _clrRED);
  Text("press any key to finish the program", 135, 10);

  (* ...en wacht tot hij/zij dat doet *)
  x := GetKey();

  (* en sluit het venster *)
  KillWindow();

END Demo2.


MODULE Demo2b;

FROM Wimdows IMPORT Window, KillWindow, Text, SetFont, GetKey, Rectangle, Circle, Line, 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;

FROM IO IMPORT WrStr, WrChar, WrCard, WrLn, RdLn, RdChar, RdCard, RdKey, KeyPressed;

CONST
  GRAPH_WIDTH = 500;  (* constantes die we gebruiken om de schermgrootte te definieren *)
  GRAPH_HEIGHT = 500;

VAR
  i, y2: CARDINAL; (* i is de controle variabele van de FOR *)
  x : CARDINAL;
BEGIN
  (* Initialisatie van de graphics *)
  Window("titel", GRAPH_WIDTH, GRAPH_HEIGHT);

  Circle(0,0,60, _clrWHITE);
    (* circel met middelpunt (x, y), straal en kleur *)

  FOR i := 1 TO 10 DO (* i gaat van 1 tot 10 *)
    y2 := i * 50;

    Line(0,0, 200, y2, _clrWHITE);
    (* lijn van (x1, y1) tot (x2, y2) in een kleur *)
    WrCard(y2,5); WrLn; (* y2 wordt afgeprint *)

  END;

  (* einde van het programma *)
  (* vraag de gebruiker een toets te drukken... *)
  SetFont(1, _clrRED);
  Text("press any key to finish the program", 135, 10);

  (* ...en wacht tot hij/zij dat doet *)
  x := GetKey();

  (* en sluit het venster *)
  KillWindow();
    
END Demo2b.

S1

Exercise

S2

Tip voor kortste oplossing:   n - ABS(i-n)
Exercise

S3

Exercise

S6

Exercise

MODULE S6;
<* NOOPTIMIZE + *>
  FROM IO IMPORT WrStr, WrLn, RdCard, WrCard, RdInt, WrInt, RdKey, RdLn;
  FROM Graph IMPORT Init, Rectangle, Circle, Line, Disc, RawOutText;
  FROM Graph IMPORT _clrBLACK, _clrWHITE, _clrGREEN, _clrCYAN, _clrMAGENTA;
  FROM Graph IMPORT _clrBROWN, _clrRED, _clrGRAY, _clrLIGHTBLUE, _clrLIGHTGREEN;

  CONST N = 5;

  VAR
   c:CHAR;
   i:CARDINAL;
BEGIN

  (* Initialise graphics. Coordinatesystem: (0,0) is the top left corner *)
  IF NOT Init(0, 0 , 420, 430) THEN
    WrStr("Sorry, graphics doesn't work");WrLn;
    RETURN;
  END;
  (* Plot figures... *)

  Line(0, 200, 200, 0, _clrWHITE);
  Line(200, 0, 400, 200, _clrWHITE);
  Line(400, 200, 200, 400, _clrWHITE);
  Line(200, 400, 0, 200, _clrWHITE);
 

  (* Show graphics until user presses a key *)
  WrStr("Press any key to finish the program");
  c := RdKey();
  WrLn; RdLn;

END S6.


X1

Exercise
 



H1

Exercise

H2

Exercise


T1

Exercise
Hint: view the program running step-by-step with the XDS debugger!!!
  1. add  <* NOOPTIMIZE + *> at the second line of your code (after MODULE) to prevent the compiler for optimising your code, because then you won't see all your code
  2. press the RUN DEBUGGER button  .
  3. put your cursor at the first green line of your code
  4. press F4, the program starts and runs until the cursor
  5. press F7 repeatedly, the program executes the next line (type a number when an input is requested!!)
  6. you see the values of the variables in the globals window


Solution:
3
33
18


T2

Exercise

Debugging tips:

Remember these tips! You will need them frequently!