Chapter 15
Answers to Exam Preparation Exercises
3. Array contents:
(a) Ex_A
COL 1 2 3
ROW
1 1 2 3
2 2 4 6
3 3 6 9
4 4 8 12
(b) Ex_B
COL 1 2 3
ROW
1 2 0 1
2 0 1 2
3 1 2 0
4 ? ? ?
(c) Ex_C
COL 1 2
ROW
1 2 3
2 2 3
3 2 3
4 2 3
5 2 3
6 2 3
7 2 3
8 4 5
6. Array Definitions:
(a) Table with 5 rows and six columns, containing Booleans:
CONST
MaxRow = 5;
MaxCol = 6;
TYPE
RowIndex = 1..MaxRow;
ColIndex = 1..MaxCol;
TableType = ARRAY[RowIndex, ColIndex] OF Boolean;
VAR
Table:
TableType;
(b) Table indexed from -5 to 0 and 'A' to 'F', that contains Real values:
TYPE
RowIndex = -5..0;
ColIndex = 'A'..'F';
TableType = ARRAY[RowIndex, ColIndex] OF Real;
VAR
Table:
TableType;
(c) A Char table with rows indexed by uppercase letters and columns by lowercase letters:
TYPE
RowIndex = 'A'..'Z';
ColIndex = 'a'..'z';
TableType = ARRAY[RowIndex, ColIndex] OF Char;
VAR
Table: TableType
Answers to Programming Warm-Up Exercises
5. Several questions in exercises 5 and 6 ask for procedures that process specific rows or columns. The solutions given are for the general case with the specific row or column passed as a parameter.
(a) Procedure to determine which team sold the most tickets during the first week:
PROCEDURE Best_Team_For_Week(CONST Tickets: Sold_Type;
Which_Week: Week_Range;
VAR Which_Team: Team_Range);
VAR
Team: Team_Range;
BEGIN (* Best_Team_For_Week *)
Which_Team := 1;
FOR Team := 2 TO Number_Of_Teams DO
IF Tickets[Team, Which_Week] >
Tickets[Which_Team, Which_Week]
THEN Which_Team := Team
END; (* Best_Team_For_Week *)
(b) Procedure to determine if a certain team sold the most tickets for any week:
FUNCTION Did_WhichTeam_Win (CONST Tickets: Sold_Type;
Which_Team: Team_Range): Boolean;
VAR
Week: Week_Range;
Week_Best: Team_Range;
Answer_Found: Boolean;
BEGIN (* Did_WhichTeam_Win *)
Answer_Found := False;
Week := 1;
REPEAT
Best_Team_For_Week(Tickets, Week, Week_Best);
IF Week_Best = Which_Team
THEN Answer_Found := True
ELSE Week := Week + 1
UNTIL Answer_Found OR (Week = Number_Of_Weeks);
IF Answer_Found
THEN Did_WhichTeam_Win := True
ELSE Did_WhichTeam_Win := False
END; (* Did_WhichTeam_Win *)
(c) Procedures to determine the week in which the most tickets were sold:
PROCEDURE Best_Week (CONST Tickets: Sold_Type;
VAR Which_Week: Week_Range);
VAR
Week: Week_Range;
Best_Sum, Sum: Integer;
PROCEDURE Sum_Week (CONST Tickets: Sold_Type;
Which_Week: Integer;
VAR Sum: Integer);
VAR
Team: Team_Range;
BEGIN (* Sum_Week *)
Sum := 0;
FOR Team := 1 TO Number_Of_Teams DO
Sum := Sum + Tickets[Team, Which_Week]
END; (* Sum_Week *)
(**************************************************)
BEGIN (* Best_Week *)
Sum_Week(Tickets, 1, Best_Sum);
Which_Week := 1;
FOR Week := 2 TO Number_Of_Weeks DO
BEGIN
Sum_Week(Tickets, Week, Sum);
IF Sum > Best_Sum
THEN
BEGIN
Best_Sum := Sum;
Which_Week := Week
END
END
END; (* Best_Week *)
(d) Procedures to determine the team that sold the most tickets:
PROCEDURE Best_Team (CONST Tickets: Sold_Type;
VAR Which_Team: Integer);
VAR
Sum, Best_Sum: Integer;
Team: Team_Range;
PROCEDURE Sum_Team (CONST Tickets: Sold_Type;
Which_Team: Integer;
VAR Sum: Integer);
VAR
Week: Week_Range;
BEGIN (* Sum_Team *)
Sum := 0;
FOR Week := 1 TO Number_Of_Weeks DO
Sum := Sum + Tickets[Which_Team,, Week]
END; (* Sum_Team *)
(*****************************************************)
BEGIN (* Best_Team *)
Sum_Team(Tickets, 1, Best_Sum);
Which_Team := 1;
FOR Team := 2 TO Number_Of_Teams DO
BEGIN
Sum_Team(Tickets, Team, Sum);
IF Sum > Best_Sum
THEN
BEGIN
Best_Sum := Sum;
Which_Team := Team
END
END
END; (* Best_Team *)
6. Procedures for sports database.
(a) Determine which school spent the most money on football:
PROCEDURE Max_Cost_For_Sport (CONST Cost_Of_Sports: Money_Type;
Which_Sport: Sport_Index:
VAR Which_School:
School_Index);
VAR
School: School_Index;
BEGIN (* Max_Cost_For_Sport *)
Which_School := 1;
FOR School := 2 TO Number_Of_Schools DO
IF Cost_Of_Sports[Which_Sport, School] >
Cost_Of_Sports[Which_Sport, Which_School]
THEN Which_School := School
END; (* Max_Cost_For_Sport *)
(b) Which sport did the last school spend the most money on:
PROCEDURE Max_Cost_For_School (CONST Cost_Of_Sports: Money_Type;
Which_School: School_Index;
VAR Which_Sport: Sport_Type);
VAR
Sport: Sport_Type;
BEGIN (* Max_Cost_For_School *)
Which_Sport := Football;
FOR Sport := Basketball TO Volleyball DO
IF Cost_Of_Sports[Sport, Which_School] >
Cost_Of_Sports[Which_Sport, Which_School]
THEN Which_Sport := Sport
END; (* Max_Cost_For_School *)
(c) Which school has the most basketball players:
PROCEDURE Most_Kids_For_Sport (CONST Kids_In_Sports: Participant_Type;
Which_Sport: Sport_Type;
VAR Which_School: School_Index);
VAR
School: School_Index;
BEGIN (* Most_Kids_In_Sport *)
Which_School := 1;
FOR School := 2 TO Number_Of_Schools DO
IF Kids_In_Sports[School, Which_Sport] >
Kids_In_Sports[Which_School, Which_Sport]
THEN Which_School := School
END; (* Most_Kids_In_Sport *)
(d) For the third school, which sport had the most kids:
PROCEDURE Most_Kids_For_School (CONST Kids_In_Sports: Participant_Type;
Which_School: School_Index;
VAR Which_Sport: Sport_Type);
VAR
Sport: Sport_Type;
BEGIN (* Most_Kids_For_School *)
Which_Sport := Football;
FOR Sport := Basketball TO Volleyball DO
IF Kids_In_Sports[Which_School, Sport] >
Kids_In_Sports[Which_School, Which_Sport]
THEN Which_Sport := Sport
END; (* Most_Kids_For_School *)
(e) Total spent on volleyball for all schools:
PROCEDURE Sum_Cost_For_Sport (CONST Cost_Of_Sports: Money_Type;
Which_Sport: Sport_Type;
VAR Sum: Real);
VAR
School: School_Index;
BEGIN (* Sum_Cost_For_Sport *)
Sum := 0.0;
FOR School := 1 TO Number_Of_Schools DO
Sum := Sum + Cost_Of_Sports[Which_Sport, School]
END; (* Sum_Cost_For_Sport *)
(f) Total students playing all sports:
PROCEDURE Sum_Kids_For_School (CONST Kids_In_Sports: Participant_Type;
Which_School: School_Index;
VAR Sum: Integer);
VAR
Sport: Sport_Type;
BEGIN (* Sum_Kids_For_School *)
Sum := 0;
FOR Sport := Football TO Volleyball DO
Sum := Sum + Kids_In_Sports[Which_School, Sport]
END; (* Sum_Kids_For_School *)
PROCEDURE Total_Kids (CONST KidslnSports: Participant_Type;
VAR Total: Integer);
VAR
School: School_Index;
School_Total: Integer;
BEGIN (* Total_Kids *)
Total := 0;
FOR School := 1 TO Number_Of_Schools DO
BEGIN
Sum_Kids_For_School(Kids_In_Sports, School,
School_Total);
Total := Total + School_Total
END
END; (* Total_Kids *)
(g) Which school had the most kids in sports:
PROCEDURE School_With_Most_Kids (CONST Kids_In_Sports:
Participant_Type;
VAR Which_School:
School_Index);
VAR
School: School_Index;
Max_Sum, Sum: Integer;
BEGIN (* School_With_Most_Kids *)
Which_School := 1;
Sum_Kids_For_School(Kids_In_Sports, 1, Max_Sum);
FOR School := 2 TO Number_Of_Schools DO
BEGIN
Sum_Kids_For_School(Kids_In_Sports, School, Sum);
IF Sum > Max_Sum
THEN
BEGIN
Max_Sum := Sum;
Which_School := School
END
END
END; (* School_With_Most_Kids *)
(h) On which sport was the most money spent:
PROCEDURE Sport_With_Most_Spent (CONST Cost_Of_Sports: Money_Type;
VAR Which_Sport: Sport_Type);
VAR
Sport: Sport_Type;
Sum, Max_Sum: Real;
BEGIN (* Sport_With_Most_Spent *)
Which_Sport := Football;
Sum_Cost_For_Sport(Cost_Of_Sports, Football, Max_Sum);
FOR Sport := Basketball TO Volleyball DO
BEGIN
Sum_Cost_For_Sport(Cost_Of_Sports, Sport, Sum);
IF Sum > Max_Sum
THEN
BEGIN
Max_Sum := Sum;
Which_Sport := Sport
END
END
END; (* Sport_With_Most_Spent *)
(i) Which sport had the most participants:
PROCEDURE Sum_Kids_For_Sport (CONST Kids_In_Sports:
Participant_Type;
Which_Sport: Sport_Type;
VAR Sum: Integer);
VAR
School: School_Index;
BEGIN (* Sum_Kids_For_Sport *)
Sum := 0;
FOR School := 1 TO Number_Of_Schools DO
Sum := Sum + Kids_In_Sports[School, Which_Sport]
END; (* Sum_Kids_For_Sport *)
PROCEDURE Sport_With_Most_Kids (CONST Kids_In_Sports:
Money_Type;
VAR Which_Sport: Sport_Type);
VAR
Sport: Sport_Type;
Sum, Max_Sum: Integer;
BEGIN (* Sport_With_Most_Kids *)
Which_Sport := Football;
Sum_Kids_For_Sport(Kids_In_Sports, Football, Max_Sum);
FOR Sport := Basketball TO Volleyball DO
BEGIN
Sum_Kids_For_Sport(Kids_In_Sports, Sport, Sum);
IF Sum > Max_Sum
THEN
BEGIN
Max_Sum := Sum;
Which_Sport := Sport
END
END
END; (* Sport_With_Most_Kids *)
9. Function to sum a given row in a table.
FUNCTION Sum_Row_I (Table: Table_Type;
Which_Row, Col_Limit: Integer): Integer;
VAR
Col, Sum: Integer;
BEGIN (* Sum_Row_I *)
Sum := 0;
FOR Col := 1 TO Table.Number_Columns DO
Sum := Sum + Table.Data[Which_Row, Col];
Sum_Row_I := Sum
END; (* Sum_Row_I *)