Chapter 10
Answers to Exam Preparation Exercises
1. The descriptions for types Real and Integer come from mathematics. The description for type Boolean comes from Boolean algebra. The description for type Char comes from the character set of a machine.
2. Both ordinal and scalar data types are atomic. An ordinal data type has the additional feature that each value, except the first, has a unique predecessor and each value, except the last, has a unique successor.
5. True
8. Succ and Pred are regular functions. Ord and Chr are conversion functions.
9. Evaluating expressions:
a. Spring b. Undefined c. 3 d. False
10. Evaluating expressions:
a. 0 b. 'B' c. 27 d. 52
14. Run time; although constant assignments can be checked at compile time.
15. False
16. True
18. True. REPEAT and UNTIL serve as the delimiters of a REPEAT loop; no BEGIN-END pair is required.
19. The FOR statement
FOR Initial := 'Z' DOWNTO 'A' DO
Write(Initia1:2)
is valid. The loop will execute 26 times, with variable Initial taking on successively the characters 'Z', 'Y', 'X', down through and including the character 'A'. In effect, the loop steps backwards through the alphabet.
21. What is printed by the program fragment:
44444
3333
222
11
22. What is printed by the program fragment is as follows (note that there will be a blank line printed following the last line of stars):
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
24. True. Values in case label lists may be in any order, but no duplicates are allowed.
25. False. However, if a case selector does not match a value on a case label lists, the results are undefined.
26. Fragment rewritten as a CASE statement:
CASE N OF
3 : Three := Three + 1;
7 : Seven := Seven + 1;
10 : Ten := Ten + 1
END;
Answers to Programming Warm-Up Exercises
1. Enumerated type for high schools:
TYPE
High_Schools = (Westlake, Lanier, Johnston, Austin,
McCallum, Crockett)
3. Subrange types of NFL teams by division:
TYPE
NationalFootball = (* as in answer to exercise 2 *);
EasternDivision = Cowboys..Redskins;
CentralDivision = Bears..Buccaneers;
WesternDivision = Falcons..SF49ers;
The teams from each division must be grouped together in order to create the subrange types.
4. Subrange consisting of uppercase letters.
TYPE
Upper_Case = 'A'..'Z'
7. Enumerated type for days of the week:
TYPE
Days_Of_Week = (Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday, Sunday)
8. Procedure to convert two letters to type Days_Of_Week:
PROCEDURE Day ( Ch1, Ch2: Char;
VAR Weekday: Days_Of_Week);
(* This procedure converts the first two letters of a *)
(* day name into a value of the enumerated type *)
(* Days_Of_Week. If the letters do not select a valid *)
(* day, Weekday is not changed *)
BEGIN (* Day *)
IF Ch1 IN ['S', 'M', 'T', 'W', 'F']
THEN
CASE Ch1 OF
'S' : IF Ch2 = 'U'
THEN Weekday := Sunday
ELSE
IF Ch2 = 'A'
THEN Weekday := Saturday;
'M' : IF Ch2 = 'O'
THEN Weekday := Monday;
'T' : IF Ch2 = 'U'
THEN Weekday := Tuesday
ELSE
IF Ch2 = 'H'
THEN Weekday := Thursday;
'W' : IF Ch2 = 'E'
THEN Weekday := Wednesday;
'F' : IF Ch2 = 'R'
THEN Weekday := Friday
END
END; (* Day *)
10. FOR loop rewritten:
S := Q1;
Flag := False;
WHILE NOT Flag DO
BEGIN
Write(Ord(S));
IF S = Q4
THEN Flag := True
ELSE S := Succ(S)
END;
11. Printing a two-digit integer as character digits:
Ch1 := Chr(N DIV 10 + Ord('0'));
Ch2 := Chr(N MOD 10 + Ord('0'));
Writeln(Ch1, Ch2)
13. Procedure Get_Yes_Or_No:
PROCEDURE Get_Yes_Or_No (VAR Ch: Char);
VAR
Valid_Char: Boolean;
BEGIN (* Get_Yes_Or_No *)
Valid_Char := False;
REPEAT
Readln(Ch);
CASE Ch OF
'N', 'Y' : Valid_Char := True
ELSE Writeln('Enter "Y" or "N".')
END;
UNTIL Valid_Char
END; (* Get_Yes_Or_No *)
16. Rewrite of FOR loop using a WHILE:
M := 93;
WHILE M >= 5 DO
BEGIN
Writeln(M, Sqr(M));
M := M - 1
END;
18. Function Power, using a FOR loop. This solution returns proper results for values of Exponent greater than or equal to zero only. It returns correct values for (Base ^ 0) = 1, (0 ^ Exponent) = 0, and the special case of (0 ^ 0) = 1.
FUNCTION Power (Base, Exponent: Integer): Integer;
VAR
Count, Temp: Integer;
BEGIN (* Power *)
Temp := 1;
FOR Count := 1 TO Exponent DO
Temp := Temp * Base;
Power := Temp
END; (* Power *)
19. CASE statement:
CASE Grade OF
'A' : Sum := Sum + 4;
'B' : Sum := Sum + 3;
'C' : Sum := Sum + 2;
'D' : Sum := Sum + 1;
'F' : Writeln('Student is on probation')
END; (* Case *)
20. Modification of exercise 19 to handle illegal values:
CASE Grade OF
'A' : Sum := Sum + 4;
'B' : Sum := Sum + 3;
'C' : Sum := Sum + 2;
'D' : Sum := Sum + 1;
'F' : Writeln('Student is on probation')
ELSE Writeln('Invalid letter grade')
END (* Case *)