CHAPTER 2

Questions

1. (a) Editor: a program that produces a text version of a program in the high level notation. (b) Compiler: a program that translates the text version into machine-readable form. (c) Linker: a program that merges previously compiled code and the current code to generate an executible program. (d) Interpreter: a program similar to a compiler but that translates code into machine language every time the program is run.

2. Input and output functions are hardware dependent, so programs written for one kind of computer are unlikely to work on another. By having I/O in the library, only the library has to be changed to port code to another computer.

3. MODULE --> name --> IMPORT commands --> BEGIN --> END.

4. Answers may vary. Check for such characters as $.

5. Answers may vary. Non-standard types may be found in SYSTEM.

6. Answers may vary.

7. Illegal identifiers: Reason: Result#1 # is not allowed Execute User space not allowed 12Dozen can't start with a digit MODULE reserved word Canada'sBest quote mark is not allowed

8. An identifier is a name for a value or an action. In Modula-2 it is a sequence of non-blank letters or digits beginning with a letter.

9. A statement is an instruction directing a computer to take some action. It usually is written out in a high level notation for subequent translation by the computer into one or more low level instructions and their corresponding actions.

10. There are two "standards", the de facto standard of classical Modula-2 as defined by Professor Wirth's books and interpreted by implementors, and the de jure standard represented by the ISO document 10514, parts 1, 2, and 3.

11. Reserved Word: A special word or maker used to outline the structure of a program in the manner of punctuation. It must be entirely written in uppercase letters, and cannot be used for any other purpose. Standard Identifer: A name that is built-in to the notation. It must be written entirely in capital letters. It can be re-used, through this is unwise Standard Library Identifier: an identifier that names a particualar library item in every standard implementation of Modula-2.

12. Literal: any entity whose name is an encoding of its value Constant: an identified value that remains the same throughout a program; that is , it is not subject to change or later re-assignment. Variable: a name for a memory location, the contents of which can be changed by a program.

13. Answers may vary.

14. Constants are declared only in the beginning of the program code. The "=" operator is used to assign names to constant values. Variables are assigned whenever needed in the program. The ":=" operator is used to assign values to variables.

15. Modula-2 expression: a combination of literals, constants, and variables using addition, subraction, multiplication, division, and/or such other operations as may be defined for the type of the entities being combined.

16. To say they are assignment but not expression compatible means that one can assign CARDINAL and INTEGER values to variables of types each other's as long as they fall in the same range, though it may cause overflow problems at run time. That they are not expression compatible, means that one cannot use the two different data types in one expression.

17. (a) card := card + VAL(CARDINAL, int); or card := card + TRUNC (int);
(b) re := FLOAT (int);
(c) lre := LFLOAT (re);
(d) lre := LFLOAT (card);
(e) re := FLOAT (card);
(f) re := FLOAT(6);
(g) This one probably cannot be fixed, unless -5 is replaced by 5 or we write int := INT(-5), both of which seem trivial.

18. (a) 36
(b) 1
(c) eror: division by 0
(d) -12
(e) -53
(f) -163.8
(g) error: real subtract whole; incompatible types
(h) 8.4
(i) error: divisor in MOD has to be positive
(j) 11.0
(k) 4.5 E+13
(l) 4.0 E+15
(m) 5.0 E-15
(n) 10.9
(o) error: divisor is a cardinal (2 different data types)
(p) error: mixing integer and cardinal types
(q) error: mixing real and longreal types.

19. Note: _ will indicate spaces.
(a) _ 4_ _-2
(b) answer_=_-6
(c) _ _1
_ _2
_ _3
...infinite loop
(d) _ _0 then the program will not write any others because counter will be an integer and WriteCard does not handle integers, so the program crashes with an overflow error.
(e) _ _3_ _6_ 12

Problems

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

(* Created
    July.07.1999
    Chapter 2, Question 21 *)

MODULE LbsToKg;

FROM STextIO IMPORT
  WriteString, WriteLn;
FROM SRealIO IMPORT
  WriteFixed;

VAR
  convert : REAL;

BEGIN
  (* Conversion: 1lb = 0.453592Kg *)
  convert = 4.0 * 0.453592;
  WriteString ("4 pounds is equivalent to");
  WriteFixed (convert, 3, 0);
END LbsToKg.

(*  Created
    July.07.1999
    Chapter 2, Question #22 *)

MODULE mphTomps;

FROM STextIO IMPORT
  WriteString, WriteLn;
FROM SRealIO IMPORT
  WriteFixed;

VAR
  convert : REAL;

BEGIN
  (* Conversion: 1mi = 1606.344m; 1hr = 3600s *)
  convert = 45.0 * 1606.344 * 3600;
  WriteString ("45 miles per hour equivalent to");
  WriteFixed (convert, 3, 0);
  WriteString (" meters per second");
END mphTomps.