CHAPTER 3

Questions

1. A BOOLEAN variable can have either a TRUE or FALSE value.

2. Order of operations for Modula-2 Boolean expressions:
First NOT (~)
Second *, /, DIV, MOD, AND (&)

Third +, -, OR

Last the relational operators =, <> (or #), >, >=, <, <=

3. (a) FALSE (b) TRUE (c) TRUE (d) TRUE

4. (a) TRUE (b) TRUE (c) TRUE (d) FALSE (e) TRUE (f) FALSE

5. Change to:

	IF finished
	  THEN
	    a := a + 1;
	END;

6.

	REPEAT
	  WriteString ("enter a cardinal here ==>");
	  ReadCard (myCard)
	  flag := ReadResult () = allRight;
	  SkipLine
	UNTIL ~flag;

7. The count will never equal 0 because decreasing an odd number by 2 will never result in an even number.

8. Unless count is a CARDINAL, nothing is wrong because the count will eventually be less than 0.

9. Variables used in the DEC (or INC) procedure have to be of type INTEGER or CARDINAL.

10. realCount = 0 has to be changed to realCount = 0.0.

11. STextIO:
ReadString: Any read into a compatible variable will be legal because all characters can be part of a string.
ReadChar: Any read will also be all right for the same reason, however only the first character will be read if there is a SkipLine following.
SWholeIO:
ReadCard;
ReadCard (x):
flag := ReadResult () = allRight;
SRealIO:
ReadReal:

ReadReal (y);
flag := ReadResult () = allRight;

12. In the import section...

	FROM RealInOut IMPORT
	  Done;
	IMPORT InOut;
	In the code...
		refer to the first as Done;
		refer to the second as InOut.Done;

13. Answers may vary.

14. Answers left to the student.

15. (i) Include temporary print statements in the code.
(ii) Test a program with alternate data.
(iii) Hand check results for reasonability.
(iv) Check loops for efficiency and correctness.
(v) Use comments.
(vi) Watch for spelling and punctuation errors.

Problems

Note: Not all problems are shown. Most problems are left up students as labs.

(*  Created
    June 10, 1999
    Chpater 3 Question 16
    ERROR TRAPPING  *)

MODULE Convert;

FROM STextIO IMPORT
  WriteString, WriteLn, ReadChar, SkipLine;
FROM SRealIO IMPORT
  ReadReal, WriteFixed;
FROM SWholeIO IMPORT
  ReadCard;
FROM SIOResult IMPORT
  ReadResult, ReadResults;

VAR
  option : CARDINAL;
  toConvert, converted : REAL;
  exit : CHAR;
  ok : BOOLEAN;

CONST
  inchFactor = 2.54;	(* conversion factor: 1in. = 2.54cm *)

BEGIN
  REPEAT
  (* Ask to choose what they want to convert *)
    REPEAT
      WriteString ("Choose one of the following options:"); WriteLn;
      WriteString ("1)    Convert inches to centimeters."); WriteLn;
      WriteString ("2)    Convert centimeters to inches."); WriteLn;
      WriteLn;
      WriteString ("Choice: ");
      ReadCard (option);
      ok := ReadResult() = allRight;

      IF NOT ok THEN
        WriteString ("Not an option!! Retry");
	WriteLn;
      END;

      SkipLine;
    UNTIL ok;

    (* Get number to convert *)
    ok := FALSE;

    WHILE NOT ok DO
      WriteString ("Enter the amount to convert: ");
      ReadReal (toConvert);
      ok := ReadResult() = allRight;

      IF NOT ok THEN
        WriteString ("Enter a proper number");
        WriteLn;
      END;

      SkipLine;
    END;

    (* Do calculations and display*)
    IF option = 1 THEN
      converted := inchFactor * toConvert;
      WriteFixed (toConvert, 2, 1);
      WriteString (" inch(es) is equivalent to");
      WriteFixed (converted, 2, 0);
      WriteString (" centimeters.");
    ELSE
      converted := toConvert/inchFactor;
      WriteFixed (toConvert, 2, 1);
      WriteString (" centimeters is equivalent to");
      WriteFixed (converted, 2, 0);
      WriteString (" inch(es).");
    END;

    (* For exiting *)
    ok := FALSE;
    WriteLn;
    WriteString ("Enter x to exit or any key run program again ");
    ReadChar (exit);
    SkipLine;

    IF (exit = 'x') OR (exit = 'X') THEN
      ok := TRUE;
    END;

    WriteLn; WriteLn;
  UNTIL ok;
END Convert.
(*	Created
	June.10.1999
	Chapter 3 Question 19
	NO ERROR TRAPPING  *)

MODULE Sort3;

FROM STextIO IMPORT
  WriteString, WriteLn, SkipLine;

FROM SWholeIO IMPORT
  ReadCard, WriteCard;

CONST
  fieldLength = 6;

VAR
  num1, num2, num3, temp : CARDINAL;

BEGIN
  (* information *)
  WriteString ("This program sorts two cardinal numbers from smallest to largest.");
  WriteLn;
  (* collect the numbers from the user *)
  WriteString ("Enter the first number please. ==> ");
  ReadCard (num1);
  SkipLine;
  WriteLn;
  WriteString ("Enter the second number please. ==> ");
  ReadCard (num2);
  SkipLine;
  WriteLn;
  WriteString ("Enter the third number please. ==> ");
  ReadCard (num3);
  SkipLine;
  WriteLn;
  WriteString ("From least to greatest, the numbers are: ");

  (* Sort them first *)
  IF num1 > num2 THEN
    temp := num2;
    num2 := num1;
    num1 := temp;
  END;

  IF num1 > num3 THEN
    temp := num3;
    num3 := num1;
    num1 := temp;
  END;

  IF num2 > num3 THEN
    temp := num2;
    num2 := num3;
    num3 := temp;
  END;
   (* Write them out *)
  WriteCard (num1, fieldLength);
  WriteString (", ");
  WriteCard (num2, fieldLength);
  WriteString (", ");
  WriteCard (num3, fieldLength);
  WriteLn;
  SkipLine;
END Sort3.