CS 241:
Control Structures
Homework 5(Solution)
Assigned: Thu. 12/02/04
Due: Thu. 12/09/04
-
[20 points]
Problems #1-#10 of Chap. #6 Chapter Review Exercises on pg. 308 of the textbook.
- [Answer]
- (#1) FOR K := 1 TO 10 DO [Valid]
- (#2) FOR J = 1 TO 5 STEP 2 DO [Invalid]
The '=' between the J and 1 should be an assignment operator
':=' and there is NO reserved word named STEP that can
be utilized within a FOR ... DO statement, therefore the above
FOR statement is illegal syntax.
- (#3) FOR J := 10 TO 1 DO [Invalid]
This specification sematically incorrect because it creates
an infinite loop given the internal increment step of the use
of the TO variation of the FOR ... DO statement.
- (#4) FOR X := 1.0 TO 3.0 DO [Invalid]
This specification is syntactically incorrect because the index
and it's corresponding initial and final values must be of type
ordinal and NOT real.
- (#5) FOR C := 'A' TO 'Z' DO [Valid]
- (#6) FOR K := A DOWNTO B DO [Invalid]
This specification is both syntactically and semantically incorrect
because character constants are required and the DOWNTO
option will result in an infinite loop.
- (#7) J := 1 TO 10 DO [Invalid]
This specification is missing the FOR reserved word
and therefore is syntactically incorrect.
- (#8) WHILE J := 1 DO [Invalid]
This specification is syntactically incorrect because the
required conditional boolean test expression is an assignment
expression.
- (#9) WHILE M = 1.5 DO [Valid]
- (#10) WHILE X <> 'A' DO [Valid]
-
[20 points]
Problems #1 and #25-#28 of Chap. #7 Exercises 7.3 on pgs 351-353 of the textbook.
- [Answer]
- (#1)
Value parameters are parameters that are copied from the memory location
of the actual parameter to the memory location of the formal parameter.
Whereas, variable parameters establish a reference that has two names for
the same memory location.
Actual parameters are those parameters that are utilized with the procedure
or function is invoked. Whereas, formal parameters are those parameters in
that are declared and utilized within the definition of a procedure or function.
- (#25) Demo (Num1, Num2);[Inappropriate]
Not enough actual arguments.
- (#26) Demo (Num1, Num2, X);[Inappropriate]
Not enough actual arguments.
- (#27) Demo (Num1, Num2, X, Ch1);[Appropriate]
- (#28) Demo (X, Y, Num1, Ch2);[Inappropriate]
Actual arguments.
-
[20 points]
Problems #4-#12 of Chap. #7 Chapter Review Exercises on pg. 371 of the textbook.
- [Answer]
- (#4) SampleProc (Int1, Real1, Ch1);[Valid]
- (#5) SampleProc;[Invalid]
This procedure invocation requires a comma separated actual parameter
list.
- (#6) SampleProc (Int1; VAR Real1, Ch1);[Invalid]
This procedure invocation requires a comma separated actual parameter
list and NO variable declarations.
- (#7) SampleProc (Int1; Real1; Ch1);[Invalid]
This procedure invocation requires a comma separated actual parameter
list.
- (#8) SampleProc (Int1, Ch1);[Invalid]
This procedure invocation requires an actual parameter list in which
both the number, order, and type must exactly match the formal parameters
of the procedure definition.
- (#9) SampleProc (3, 5.0, 'X');[Valid]
- (#10) SampleProc (Real1, Int1, Ch1);[Invalid]
This procedure invocation requires an actual parameter list in which
both the number, order, and type must exactly match the formal parameters
of the procedure definition.
- (#11) SampleProc (Int1, Int2, Int3);[Invalid]
This procedure invocation requires an actual parameter list in which
both the number, order, and type must exactly match the formal parameters
of the procedure definition.
- (#12) SampleProc (Real1, Int1, Ch1);[Invalid]
This procedure invocation requires an actual parameter list in which
both the number, order, and type must exactly match the formal parameters
of the procedure definition.
-
[20 points]
Problems #15-#21 of Chap. #7 Chapter Review Exercises on pg. 371 of the textbook.
- [Answer]
- (#15)
PROCEDURE Exercise15 (A : integer, Y : real);[Invalid]
The formal parameter declaration list within a procedure heading must be
seperated by semi-colons ';' and NOT commas ','.
- (#16)
PROCEDURE Exercise16 (VAR A; X : integer);[Invalid]
The formal parameter declaration list within a procedure heading must
specify the data type for each formal parameter.
- (#17)
PROCEDURE Exercise17 (VAR A : integer; VAR B : integer);[Valid]
- (#18)
PROCEDURE (VAR A : integer; B : char);[Invalid]
The procedure heading must contain a procedure name.
- (#19)
PROCEDURE Exercise19 (VAR A : integer: VAR B : integer);[Invalid]
The formal parameter declaration list within a procedure heading must be
seperated by semi-colons ';' and NOT colons ':'.
- (#20)
PROCEDURE Exercise20 (A : integer; VAR B : char);[Valid]
- (#21)
PROCEDURE Exercise21 (VAR A : integer; VAR C : char);[Valid]
-
[20 points]
Problems #27-#33 of Chap. #7 Chapter Review Exercises on pgs 371-372 of the textbook.
- [Answer]
- (#27) FUNCTION Exercise27 (X : real);[Invalid]
A valid FUNCTION heading must contain/specify a return type.
- (#28) FUNCTION Exercise28;[Invalid]
A valid FUNCTION heading must contain/specify a return type.
- (#29)
FUNCTION Exercise29 (VAR A : integer) : integer;[Valid]
- (#30)
FUNCTION Exercise30 (X : real) : char;[Valid]
- (#31)
FUNCTION Exercise31 (A : integer; B : real) : boolean;[Valid]
- (#32)
FUNCTION Exercise32 (A : integer; B : real) : integer;[Valid]
- (#33)
FUNCTION Exercise33 (A, B : real; C : integer) ; integer;[Invalid]
A valid FUNCTION heading must contain/specify a return type that is delimited
by a colon ':' and NOT a semi-colon ';'.
This page is maintained by Christopher A. Gantz (cgantz@regis.edu).