1. Name the five steps to top-down problem solving as described in the text.
    (It's okay if you can't remember the exact wording;
    the concepts are more important.)
    ___Write___ _Apprehend_ __Design___ _Execute___ _Scrutinize
  2. Name the five computer hardware abstractions described in the text. (Again, concepts are more important than exact wording.)
    __Input____ _Memory____ _Processor_ __Control__ __Output___
  3. Write "yes" or "no" next to each of the following six strings to indicate if it is a legal Modula-2 identifier.
    numApples _yes__ sockeye-salmon _no___ 95percent _no___
    DO _no___ L337_hAX0rz _yes__ Module _yes__
  4. Evaluate each of the following four Boolean expressions, or if they give an error, indicate why.
    ~(CAP('q') <> 'Q') _________TRUE____________
    (5 / 11 = 2) OR NOT FALSE AND FALSE _________FALSE___________
    0#0&~0/0<0 _invalid operand: NOT 0__
    (6 REM 4 > 2) AND (2 REM 0 = 2) _________FALSE___________
  5. Given the following declarations:
    	card1, card2 : CARDINAL;
    	int1, int2 : INTEGER;
    	real1, real2 : REAL;	
    Indicate whether each of the following five statements is "okay", "expression incompatible", or "assignment incompatible". A statement cannot be both expression and assignment incompatible; the first error returned is expression incompatibility. Assume all the variables have been initialized already.
    int1 := int2 - 56; _____okay______________
    int1 := card2 DIV 7; _____okay______________
    real1 := real2 + 23; _expression incompat___
    int1 := VAL(CARDINAL, 52.3); _____okay______________
    real1 := TRUNC(real2); _assignment incompat___
  6. What is wrong with this loop? How would you fix it?
    MODULE BuggyLoop;
    VAR
    	counter : CARDINAL;
    BEGIN
    	counter := 1;
    	REPEAT				(* count up to 10 by twos *)
    		statement sequence;
    		counter := counter + 2;
    	UNTIL counter >= 10;		(* counter never hits 10 exactly *)
    END BuggyLoop.
    
  7. In the space below, write pseudocode for a function procedure GetReal that does robust interactive user input:

    The procedure should prompt the user to enter a positive real value, which is then passed back to the calling context via one of GetReal's parameters. If the user does not enter a positive value, print an error message and prompt the user again. Another parameter should specify how many times to keep trying before giving up. The function should also return TRUE if the user eventually typed a good value, or FALSE if it had to give up.

  8. On a separate sheet of paper, write a complete Modula-2 module, declaring the procedure GetReal. The body of the module should have a few different invocations of GetReal to test it out.

    Hints: What parameters does GetReal need? Call-by-value or call-by-reference? What return type should it use? Be careful to choose different names for formal parameters and actual parameters.

GetReal Pseudocode:

GetReal Code:

(* This module is a test suite for the GetReal function,
 * which repeatedly tries to get the user to input a positive real.
 * Author: Sean Ho
 *)
MODULE GetRealTest;

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

VAR
	userInput : REAL;

(* This procedure prompts the user to type in a positive real.
 * If the user types something negative, we try again, up to maxTries times.
 * The procedure returns TRUE if we got a positive value, or FALSE if
 * we gave up after maxTries attempts.
 * Pre: maxTries > 0 (maxTries = 0 means we don't even try once)
 * Post: if the function returns TRUE, then result holds a positive real.
 *)
PROCEDURE GetReal (VAR result : REAL; maxTries : CARDINAL) : BOOLEAN;
VAR
	try : CARDINAL;
BEGIN
	try := 1;
	WHILE try <= maxTries
		DO
			WriteString ("Please type a positive number: ");
			ReadReal (result);
			SkipLine;
			IF result >= 0
				THEN
					WriteString ("Thanks!");
					WriteLn;
					RETURN TRUE;
				END;
			WriteString ("Sorry, please try again.  ");
			INC (try);
		END;
	WriteString ("Bah, I give up!!");
	WriteLn;
	RETURN FALSE;
END GetReal;

BEGIN
	WriteString ("Let's test GetReal with one try.");
	WriteLn;
	IF GetReal (userInput, 1)
		THEN
			WriteString ("Here's what GetReal produced:");
			WriteFixed (userInput, 2, 0);
			WriteLn;
		ELSE
			WriteString ("Aww, GetReal gave up on you.");
			WriteLn;
		END;
	WriteString ("Let's test GetReal with three tries.");
	WriteLn;
	IF GetReal (userInput, 3)
		THEN
			WriteString ("Here's what GetReal produced:");
			WriteFixed (userInput, 2, 0);
			WriteLn;
		ELSE
			WriteString ("Aww, GetReal gave up on you.");
			WriteLn;
		END;
END GetRealTest.