Chapter 13
Answers to Exam Preparation Exercises
2. Algorithm sketches:
(a) The indices of the Sales array have semantic content. After setting Sales to all zeros, read in a Code and an Amount. The statement
Sales[Code] := Sales[Code] + Amount
will increment the total for the proper category.
(b) This is an arrays of records problem. The master file must be read into an array of records. The second file is read one record at a time. The ID number of the current record is used to find the associated payroll information in the array of records.
3. (a) valid (b) valid (c) valid (d) valid (e) invalid (f) invalid (g) valid
5. Declarations
CONST
Max_Number = 1000;
TYPE
(a) Car_Type = (Ford, Honda, Jaguar, Renault, Saab);
(b) Inventory_Range = 1..Max_Number;
Inventory = ARRAY[Inventory_Range] OF Car_Type;
(c) VAR
Cars : Inventory;
6. (a) valid (b) invalid (c) valid (d) invalid (e) valid (f) invalid
7. (a) Print each book borrowed by Name
FOR Counter := 1 TO Books.Length DO
IF Books.Borrower[Counter] = Name
THEN Write(Books.Title[Counter])
(b) Count books borrowed by Name
Books_Borrowed := 0;
FOR Counter := 1 TO Books.Length DO
IF Books.Borrower[Counter] = Name
THEN Books_Borrowed := Books_Borrowed + 1
(c) Count number of copies of Book_In borrowed
Number_Of_Copies := 0;
FOR Counter := 1 TO Books.Length DO
IF Books.Title[Counter] = Book_In
THEN Number_Of_Copies := Number_Of_Copies + 1
(d) Count copies of Book_In borrowed by Name
Number_Of_Copies := 0;
FOR Counter := 1 TO Books.Length DO
IF (Books.Title[Counter] = Book_In) AND
(Books.Borrower[Counter] = Name)
THEN Number_Of_Copies := Number_Of_Copies + 1
(e) Print the names of those who have books checked out
FOR Counter := 1 TO Books.Length DO
Writeln(Books.Borrower[Counter])
(f) Does Chipendale have a book checked out?
Name := 'Chipendale ';
CheckedOut := False;
Counter := 0;
WHILE NOT CheckedOut AND Counter <= Books.Length DO
IF Books.Borrower[Counter] = Name
THEN CheckedOut := True
ELSE Counter := Counter + 1;
IF CheckedOut
THEN Writeln('Chipendale has a book checked out.')
ELSE Writeln('Chipendale doesn't have a book checked out.');
(g) Is Gone with the Wind checked out?
Book_In := 'Gone with the Wind ';
CheckedOut := False;
Counter := 0;
WHILE NOT CheckedOut AND Counter <= Books.Length DO
IF Books.Title[Counter] = Book_In
THEN CheckedOut := True
ELSE Counter := Counter + 1;
IF CheckedOut
THEN Writeln('Gone with the Wind is checked out.')
ELSE Writeln('Gone with the Wind is not checked out.');
9. Number of comparisons:
Item Is_There2 Is_There_Ordered
28 1 1
32 10 2
196 7 7
194 10 7
Answers to Programming Warm-Up Exercises
2. Procedure Take_Product:
PROCEDURE Take_Product ( A, B : Int_Ary;
Vector_Length : Integer;
VAR Product : Integer);
VAR
Counter: Integer;
BEGIN (* Take_Product *)
Product := 1;
FOR Counter := 1 TO Vector_Length DO
IF A[Counter] < 0
THEN Product := Product * B[Counter]
END; (* Take_Product *)
3. Function Found.
FUNCTION Found (List : ListType; Item: Real) : Boolean
VAR
Counter : Integer;
BEGIN (* Found *)
Found := False;
Counter := 1;
WHILE NOT Found AND (Counter <= List.Length) DO
IF Item = List.Items[Counter]
THEN Found := True
ELSE Counter := Counter + 1
END; (* Found *)
6. No changes are necessary.
7. Procedure to delete all occurrences of Item:
PROCEDURE Delete_All (VAR List: List_Type;
Item: Item_Type);
(* Pre: List has been initialized *)
(* Post: No copies of item exist in List *)
VAR
Found: Boolean;
Counter: Integer;
BEGIN (* Delete_All *)
REPEAT
Counter := 1;
Found := False;
WHILE (Counter <= List.Length) AND NOT Found DO
IF List.Items[Counter] = Item
THEN Found := True
ELSE Counter := Counter + 1;
IF Found
THEN
BEGIN
FOR Counter := Counter TO List.Length - 1 DO
List[Counter] := List[Counter + 1];
List.Length := List.Length - 1
END
UNTIL NOT Found
END; (* Delete_All *)
8. Procedure Copy_List
PROCEDURE Copy_List(VAR Out_List : List_Type;
In_List : List_Type);
BEGIN (* Copy_List *)
Out_List := In_List
END; (* Copy_List *)
In Chapter 17 we introduce another list implementation for which a simple assignment statement doesn't work.
9. Parallel list example:
PROCEDURE Set_Score (VAR Score: Real_List_Type;
Present: Boolean_List_Type);
(* Pre: Score and Present are the same length *)
VAR
Counter: Integer;
BEGIN (* Set_Score *)
FOR Counter := 1 TO Present.Length DO
IF Present.Items[Counter]
THEN Score.Items[Counter] := 0.0
END; (* Set_Score *)
12. Alternate insertion algorithm
PROCEDURE Insert_Alt(VAR List: List_Type; Item: Item_Type);
VAR
Counter : Integer;
Place_Found : Boolean;
BEGIN (* Insert_Alt *)
Counter := 1;
WHILE NOT Place_Found AND (Counter <= List.Length) DO
IF Item < List.Items[Counter]
THEN Counter := Counter + 1
ELSE Place_Found := True;
FOR Counter := List.Length DOWNTO Counter DO
List.Items[Counter + 1] := List.Items[Counter];
List.Items[Counter] := Item;
List.Length := List.Length + 1
END; (* Insert_Alt *)
13. Comparison of two lists
FUNCTION Identical(List1, List2 : List_Type) : Boolean;
VAR
Counter : Integer;
Still_The_Same : Boolean;
BEGIN (* Identical *)
Counter := 1;
Still_The_Same := List1.Length = List2.Length;
WHILE Still_The_Same AND (Counter <= List1.Length) DO
IF List1.Items[Counter] <> List2.Items[Counter]
THEN Still_The_Same := False
ELSE Counter := Counter + 1;
Identical := Still_The_Same
END; (* Identical *)