-
Describe the five program structure/flow
abstractions mentioned in the text.
_SEQUENCE__ |
_SELECTION_ |
_REPETITION |
COMPOSITION |
PARALLELISM |
- Fill in the blank:
- "Computers are TOOLS, and computing scientists are TOOLSMITHS."
- A procedure that returns a value is called a FUNCTION procedure.
- Before using the value of a variable for the first time in a program,
we need to DECLARE and INITIALIZE it.
- TRUE: The word FUNCTION is a legal identifier.
- The three loop structures in Modula-2 are
WHILE/DO/END, REPEAT/DO/UNTIL, FOR/DO/END.
- A new type made from a sequence of consecutive values of an
existing type is called a SUBTYPE of the host type.
- What English part of speech should a variable generally be? NOUN
- The ABS function is used to determine how far a
real/integer is from zero.
- The function that evaluates natural logarithms on real numbers is found
in the REALMATH standard library.
- Redirection of the standard input/output channels is handled by the
REDIRSTDIO library.
- A procedure that invokes itself is called a RECURSIVE procedure.
-
Explain all the differences between the following, and give examples of each:
- reserved words: Syntactical punctuation, part of the language. e.g., MODULE, BEGIN, VAR
- standard identifiers: Names builtin to the language, e.g. names of builtin types. e.g. CHAR, CARDINAL, REAL
- standard library items: Names of procedures/types/variables included in the standard library modules that come with every ISO Modula-2 installation. e.g., WriteString, ReadReal.
-
Write a complete declaration for each of the following data types:
- A string type: TYPE String = ARRAY [0..10] OF CHAR;
- An enumeration type representing the months of the year: TYPE Month = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
- A type holding one string for each month of the year: TYPE MonthString = ARRAY Month OF String;
- A type for only the uppercase letters: TYPE UpperCase = ['A'..'Z'];
- A type for an m by n matrix of booleans: TYPE BoolMatrix = ARRAY [1..m] OF ARRAY [1..n] OF BOOLEAN
-
Evaluate each of the following expressions,
or if it produces an error, describe the error.
Assume all necessary IMPORTs have been done.
Assume the following declaration and initialization:
myApple : ARRAY [0..5] OF CHAR;
myApple := "granny smith";
- 11 REM 0 : ERROR (DIVIDE BY ZERO)
- 12.2 MOD 5 : ERROR (MOD NOT DEFINED ON REALS)
- WriteFixed (23.468, 4, 8); : " 23.4680" (one space before)
- ~(3<5)&~3#5: ERROR (NOT 3) (NOT IS NOT DEFINED ON CARDINALS)
- LENGTH(myApple) : 6
- (LENGTH(myApple) > 0) AND (CAP(myApple[0]) = 'G') : TRUE
- Strings.Compare (myApple, "Granny Smith") : GREATER
- 'C' > 'A' : TRUE
- 'C' - 'A' : ERROR (MINUS NOT DEFINED ON CHAR)
- ORD ('C') - ORD ('A') : 2
- 'C' + 'A' : "CA" (string)
- ORD (March) (using the enumeration type you defined above): 2
-
Show the results (output and/or errors) both with and without the VAR in the
formal parameter list and explain.
MODULE SwapTest;
FROM SWholeIO IMPORT
WriteCard;
PROCEDURE Swap (VAR a, b: CARDINAL);
VAR
tmp : CARDINAL;
BEGIN
tmp := a;
a := b;
b := tmp;
END Swap;
VAR
a, b: CARDINAL;
BEGIN
a := 2;
b := 5;
Swap (a, b);
WriteCard (a, 0);
WriteCard (b, 0);
END SwapTest.
With the VAR, Swap works as expected: " 5 2" is the output.
Without the VAR, the values of a and b in the global context are unchanged; the output is " 2 5".
-
Consider the following procedure:
PROCEDURE SumPlus1 (VAR a, b: CARDINAL): CARDINAL;
(* returns a+b+1 *)
BEGIN
INC (a);
RETURN a + b;
END SumPlus1;
Assume there is a global variable x := 5. If we invoke
SumPlus1 (x, x), we might expect the result to be 11,
but it is not. What is returned instead, and why? How would you fix this?
The result of SumPlus1(x,x) is 12, because both formal parameters a and b are aliases for the actual parameter x. So after the INC(a), both a and b have value 6. If we want to preserve the semantics of SumPlus1 returning a+b+1 without modifying the actual parameter, we could just take out the VAR in the formal parameter list. Alternately, we could just make b a value parameter and leave a as a variable parameter; that way, the actual parameter linked to a still gets incremented. Either solution is acceptable.