Chapter 13

MULTIDIMENSIONAL ARRAYS

EXERCISE ANSWERS

Exam Preparation Exercises

2. a. 10 b. 3 c. 3 d. 10 e. 30 f. 30 g. row h. row

4. a. enum TeamType {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR, POST_GRAD};

enum ResultType {WON, LOST, TIED};

b. typedef int Outcome[5][3];

c. Outcome standings;

d. standings[FRESHMAN][WON]++;

6. a. valid

b. invalid (27 is outside the column range)

c. valid

d. invalid (aggregate array input is defined only for a one-dimensional char array—si.e., a string)

e. valid (nameList[0] denotes a one-dimensional char array—i.e., a string)

f. valid

7. a. Boolean table[5][6];

b. float table[40][200];

c. char table[4][16];

9. a. float arrayA[10][7][21];

b. float arrayB[50][50][128][128];

Programming Warm-Up Exercises

1. Boolean Positive( /* in */ const TableType table,

/* in */ int rowLength,

/* in */ int colLength )

// Precondition:

// rowLength <= NUM_ROWS && colLength <= NUM_COLS

// && table[0..rowLength-1][0..colLength-1] are assigned

// Postcondition:

// Function value == TRUE, if all values in

// table[0..rowLength-1][0..colLength-1]

// are positive

// == FALSE, otherwise

{

int row = 0; // Loop control and index variable

int col; // Loop control and index variable

Boolean allPos = TRUE; // TRUE if all values are positive

while (row < rowLength && allPos)

{

// Invariant (prior to test):

// table[0..row-1][0..colLength-1] are positive

// && 0 <= row <= rowLength

col = 0;

while (col < colLength && allPos)

// Invariant (prior to test):

// table[row][0..col-1] are positive

// && 0 <= col <= colLength

if (table[row][col] <= 0)

allPos = FALSE;

else

col++;

row++;

}

return allPos;

}

3. void Copy( /* in */ const DataType data,

/* out */ DataType data2 )

// Precondition:

// data[0..NUM_ROWS-1][0..NUM_COLS-1] are assigned

// Postcondition:

// data2[0..NUM_ROWS-1][0..NUM_COLS-1] ==

// corresponding elements of array "data"

{

int row; // Loop control and index variable

int col; // Loop control and index variable

for (row = 0; row < NUM_ROWS; row++)

// Invariant (prior to test):

// data[0..row-1][0..NUM_COLS-1] == corresponding

// elements of array "data"

// && 0 <= row <= NUM_ROWS

for (col = 0; col < NUM_COLS; col++)

// Invariant (prior to test):

// data[row][0..col-1] == corresponding

// elements of array "data"

// && 0 <= col <= NUM_COLS

data2[row][col] = data[row][col];

}

4. float Largest( /* in */ const float table[][50] )

// Precondition:

// table[0..49][0..49] are assigned

// Postcondition:

// Function value == largest value in table[0..49][0..49]

{

int row; // Loop control and index variable

int col; // Loop control and index variable

float maxSoFar = table[0][0]; // Maximum value so far

for (row = 0; row < 50; row++)

// Invariant (prior to test):

// maxSoFar == largest value in table[0..row-1][0..49]

// && 0 <= row <= 50

for (col = 0; col < 50; col++)

// Invariant (prior to test):

// maxSoFar == largest value in

// table[0..row-1][0..49] and

// table[row][0..col-1]

// && 0 <= col <= 50

if (table[row][col] > maxSoFar)

maxSoFar = table[row][col];

return maxSoFar;

}

5. a. int BestTeam( /* in */ const int tickets[][NUM_WEEKS] )

// Precondition:

// tickets[0..NUM_TEAMS-1][0..NUM_WEEKS-1] are assigned

// Postcondition:

// Function value == i, where tickets[i][0] is the largest

// value in tickets[0..NUM_TEAMS-1][0]

{

int team; // Loop control and index variable

int indexOfMax = 0; // Index of max. value in column 0

for (team = 1; team < NUM_TEAMS; team++)

// Invariant (prior to test):

// tickets[indexOfMax][0] == largest value in

// tickets[0..team-1][0]

// && 1 <= team <= NUM_TEAMS

if (tickets[team][0] > tickets[indexOfMax][0])

indexOfMax = team;

return indexOfMax;

}

b. int BestWeek( /* in */ const int tickets[][NUM_WEEKS] )

// Precondition:

// tickets[0..NUM_TEAMS-1][0..NUM_WEEKS-1] are assigned

// Postcondition:

// Function value == i, where tickets[1][i] is the largest

// value in tickets[1][0..NUM_WEEKS-1]

{

int week; // Loop control and index variable

int indexOfMax = 0; // Index of max. value in row 1

for (week = 1; week < NUM_WEEKS; week++)

// Invariant (prior to test):

// tickets[1][indexOfMax] == largest value in

// tickets[1][0..week-1]

// && 1 <= week <= NUM_WEEKS

if (tickets[1][week] > tickets[1][indexOfMax])

indexOfMax = week;

return indexOfMax;

}

c. The following solution uses two functions, the first of which computes the column sum of a specified column of the array.

int ColSum( /* in */ const int tickets[][NUM_WEEKS],

/* in */ int whichCol )

// Precondition:

// whichCol < NUM_WEEKS

// && tickets[0..NUM_TEAMS-1][whichCol] are assigned

// Postcondition:

// Function value == tickets[0][whichCol] + ...

// + tickets[NUM_TEAMS-1][whichCol]

{

int row; // Loop control and index variable

int sum = 0; // Accumulating sum

for (row = 0; row < NUM_TEAMS; row++)

// Invariant (prior to test):

// sum == tickets[0][whichCol] + ...

// + tickets[row-1][whichCol]

// && 0 <= row <= NUM_TEAMS

sum = sum + tickets[row][whichCol];

return sum;

}

//****************************************************************

int WinningWeek( /* in */ const int tickets[][NUM_WEEKS] )

// Precondition:

// tickets[0..NUM_TEAMS-1][0..NUM_WEEKS-1] are assigned

// Postcondition:

// Function value == i, where column i of array "tickets" has

// the greatest column sum

{

int week; // Loop control and index variable

int sum; // A column sum

int maxSum; // Maximum column sum so far

int colOfMax; // Index of column with maximum sum

maxSum = ColSum(tickets, 0);

colOfMax = 0;

for (week = 1; week < NUM_WEEKS; week++)

{

// Invariant (prior to test):

// Column colOfMax has the greatest col. sum of

// columns 0 through week-1

// && maxSum == col. sum of column colOfMax

// && 1 <= week <= NUM_WEEKS

sum = ColSum(tickets, week);

if (sum > maxSum)

{

maxSum = sum;

colOfMax = week;

}

}

return colOfMax;

}

d. The following solution uses two functions, the first of which computes the row sum of a specified row of the array.

int RowSum( /* in */ const int tickets[][NUM_WEEKS],

/* in */ int whichRow )

// Precondition:

// whichRow < NUM_TEAMS

// && tickets[whichRow][0..NUM_WEEKS-1] are assigned

// Postcondition:

// Function value == tickets[whichRow][0] + ...

// + tickets[whichRow][NUM_WEEKS-1]

{

int col; // Loop control and index variable

int sum = 0; // Accumulating sum

for (col = 0; col < NUM_WEEKS; col++)

// Invariant (prior to test):

// sum == tickets[whichRow][0] + ...

// + tickets[whichRow][col-1]

// && 0 <= col <= NUM_WEEKS

sum = sum + tickets[whichRow][col];

return sum;

}

//****************************************************************

int WinningTeam( /* in */ const int tickets[][NUM_WEEKS] )

// Precondition:

// tickets[0..NUM_TEAMS-1][0..NUM_WEEKS-1] are assigned

// Postcondition:

// Function value == i, where row i of array "tickets" has

// the greatest row sum

{

int team; // Loop control and index variable

int sum; // A row sum

int maxSum; // Maximum row sum so far

int rowOfMax; // Index of row with maximum sum

maxSum = RowSum(tickets, 0);

rowOfMax = 0;

for (team = 1; team < NUM_TEAMS; team++)

{

// Invariant (prior to test):

// Row rowOfMax has the greatest row sum of

// rows 0 through team-1

// && maxSum == row sum of row rowOfMax

// && 1 <= team <= NUM_TEAMS

sum = RowSum(tickets, team);

if (sum > maxSum)

{

maxSum = sum;

rowOfMax = team;

}

}

return rowOfMax;

}

6. The RowSum and ColSum functions of Exercise 5 can be adapted so that they can be called from the following functions. The data types of variables and the initialization and termination of the For loops would need to be changed. These functions are not repeated here.

a. int SchoolMostMoneyFootball(

/* in */ const float costOfSports[][NUM_SCHOOLS] )

// Precondition:

// costOfSports[FOOTBALL..VOLLEYBALL][0..NUM_SCHOOLS-1] are

// assigned

// Postcondition:

// Function value == i, where costOfSports[FOOTBALL][i] is

// the largest value in

// costOfSports[FOOTBALL][0..NUM_SCHOOLS-1]

{

int school; // Loop control and index variable

int indexOfMax = 0; // Index of max. value in row FOOTBALL

for (school = 1; school < NUM_SCHOOLS; school++)

// Invariant (prior to test):

// costOfSports[FOOTBALL][indexOfMax] ==

// largest value in

// costOfSports[FOOTBALL][0..school-1]

// && 1 <= school <= NUM_SCHOOLS

if (costOfSports[FOOTBALL][school] >

costOfSports[FOOTBALL][indexOfMax])

indexOfMax = school;

return indexOfMax;

}

b. SportType SportMostMoneyLast(

/* in */ const float costOfSports[][NUM_SCHOOLS] )

// Precondition:

// costOfSports[FOOTBALL..VOLLEYBALL][0..NUM_SCHOOLS-1] are

// assigned

// Postcondition:

// Function value == i,

// where costOfSports[i][NUM_SCHOOLS-1] is the largest value

// in costOfSports[FOOTBALL..VOLLEYBALL][NUM_SCHOOLS-1]

{

SportType indexOfMax = FOOTBALL; // Index of max. value in

// column NUM_SCHOOLS-1

if (costOfSports[BASKETBALL][NUM_SCHOOLS-1] >

costOfSports[indexOfMax][NUM_SCHOOLS-1])

indexOfMax = BASKETBALL;

if (costOfSports[VOLLEYBALL][NUM_SCHOOLS-1] >

costOfSports[indexOfMax][NUM_SCHOOLS-1])

indexOfMax = VOLLEYBALL;

return indexOfMax;

}

c. int SchoolMostKidsBasketball(

/* in */ const int kidsInSports[][NUM_SPORTS] )

// Precondition:

// kidsInSports[0..NUM_SCHOOLS-1][FOOTBALL..VOLLEYBALL] are

// assigned

// Postcondition:

// Function value == i,

// where kidsInSports[i][BASKETBALL] is the largest value

// in kidsInSports[0..NUM_SCHOOLS-1][BASKETBALL]

{

int school; // Loop control and index variable

int indexOfMax = 0; // Index of max. value in

// column BASKETBALL

for (school = 1; school < NUM_SCHOOLS; school++)

// Invariant (prior to test):

// kidsInSports[indexOfMax][BASKETBALL] ==

// largest value in

// kidsInSports[0..school-1][BASKETBALL]

// && 1 <= school <= NUM_SCHOOLS

if (kidsInSports[school][BASKETBALL] >

kidsInSports[indexOfMax][BASKETBALL])

indexOfMax = school;

return indexOfMax;

}

d. SportType SportMostKidsThird(

/* in */ const int kidsInSports[][NUM_SPORTS] )

// Precondition:

// kidsInSports[0..NUM_SCHOOLS-1][FOOTBALL..VOLLEYBALL] are

// assigned

// Postcondition:

// Function value == i,

// where kidsInSports[2][i] is the largest value

// in kidsInSports[2][FOOTBALL..VOLLEYBALL]

{

SportType indexOfMax = FOOTBALL; // Index of max. value

// in row 2

if (kidsInSports[2][BASKETBALL] > kidsInSports[2][indexOfMax])

indexOfMax = BASKETBALL;

if (kidsInSports[2][VOLLEYBALL] > kidsInSports[2][indexOfMax])

indexOfMax = VOLLEYBALL;

return indexOfMax;

}

e. float TotalSpentOnVolleyball(

/* in */ const float costOfSports[][NUM_SCHOOLS] )

// Precondition:

// costOfSports[FOOTBALL..VOLLEYBALL][0..NUM_SCHOOLS-1] are

// assigned

// Postcondition:

// Function value == sum of values in

// costOfSports[VOLLEYBALL][0..NUM_SCHOOLS-1]

{

return RowSum(costOfSports, VOLLEYBALL);

}

f. int TotalKidsInSports(

/* in */ const int kidsInSports[][NUM_SPORTS] )

// Precondition:

// kidsInSports[0..NUM_SCHOOLS-1][FOOTBALL..VOLLEYBALL] are

// assigned

// Postcondition:

// Function value == sum of all elements of kidsInSports

{

SportType sport; // Loop control and index variable

int total = 0; // Total number of kids

for (sport = FOOTBALL; sport <= VOLLEYBALL;

sport = SportType(sport + 1))

// Invariant (prior to test):

// total == sum of all values in columns

// FOOTBALL through sport-1

// && FOOTBALL <= sport <= VOLLEYBALL+1

total = total + ColSum(kidsInSports, sport);

return total;

}

g. int SchoolMostKidsInSports(

/* in */ const int kidsInSports[][NUM_SPORTS] )

// Precondition:

// kidsInSports[0..NUM_SCHOOLS-1][FOOTBALL..VOLLEYBALL] are

// assigned

// Postcondition:

// Function value == i, where row i of kidsInSports has

// the greatest row sum

{

int school; // Loop control and index variable

int sum; // A row sum

int maxSum; // Maximum row sum so far

int rowOfMax; // Index of row with maximum sum

maxSum = RowSum(kidsInSports, 0);

rowOfMax = 0;

for (school = 1; school < NUM_SCHOOLS; school++)

{

// Invariant (prior to test):

// Row rowOfMax has the greatest row sum of

// rows 0 through school-1

// && maxSum == row sum of row rowOfMax

// && 1 <= school <= NUM_SCHOOLS

sum = RowSum(kidsInSports, school);

if (sum > maxSum)

{

maxSum = sum;

rowOfMax = school;

}

}

return rowOfMax;

}

h. SportType SportMostMoney(

/* in */ const float costOfSports[][NUM_SCHOOLS] )

// Precondition:

// costOfSports[FOOTBALL..VOLLEYBALL][0..NUM_SCHOOLS-1] are

// assigned

// Postcondition:

// Function value == i, where row i of costOfSports has

// the greatest row sum

{

SportType sport; // Loop control and index variable

float sum; // A row sum

float maxSum; // Maximum row sum so far

SportType rowOfMax; // Index of row with maximum sum

maxSum = RowSum(costOfSports, FOOTBALL);

rowOfMax = FOOTBALL;

for (sport = BASKETBALL; sport <= VOLLEYBALL;

ssport = SportType(sport + 1))

{

// Invariant (prior to test):

// Row rowOfMax has the greatest row sum of

// rows FOOTBALL through sport-1

// && maxSum == row sum of row rowOfMax

// && BASKETBALL <= sport <= VOLLEYBALL+1

sum = RowSum(costOfSports, sport);

if (sum > maxSum)

{

maxSum = sum;

rowOfMax = sport;

}

}

return rowOfMax;

}

i. SportType SportMostKids(

/* in */ const int kidsInSports[][NUM_SPORTS] )

// Precondition:

// kidsInSports[0..NUM_SCHOOLS-1][FOOTBALL..VOLLEYBALL] are

// assigned

// Postcondition:

// Function value == i, where column i of kidsInSports has

// the greatest column sum

{

SportType sport; // Loop control and index variable

int sum; // A column sum

int maxSum; // Maximum column sum so far

SportType colOfMax; // Index of column with maximum sum

maxSum = ColSum(kidsInSports, FOOTBALL);

colOfMax = FOOTBALL;

for (sport = BASKETBALL; sport <= VOLLEYBALL;

sport = SportType(sport + 1))

{

// Invariant (prior to test):

// Column colOfMax has the greatest col. sum of

// columns FOOTBALL through sport-1

// && maxSum == col. sum of column colOfMax

// && BASKETBALL <= sport <= VOLLEYBALL+1

sum = ColSum(kidsInSports, sport);

if (sum > maxSum)

{

maxSum = sum;

colOfMax = sport;

}

}

return colOfMax;

}

7. void InitSales( /* out */ SalesType sales )

// Postcondition:

// sales[0..NUM_STORES-1][0..NUM_MONTHS-1][0..NUM_DEPTS-1] == 0

{

int store; // Loop control and index variable

int month; // Loop control and index variable

int dept; // Loop control and index variable

for (store = 0; store < NUM_STORES; store++)

// Invariant (prior to test):

// sales[0..store-1][0..NUM_MONTHS-1][0..NUM_DEPTS-1] == 0

// && 0 <= store <= NUM_STORES

for (month = 0; month < NUM_MONTHS; month++)

// Invariant (prior to test):

// sales[store][0..month-1][0..NUM_DEPTS-1] == 0

// && 0 <= month <= NUM_MONTHS

for (dept = 0; dept < NUM_DEPTS; dept++)

// Invariant (prior to test):

// sales[store][month][0..dept-1] == 0

// && 0 <= dept <= NUM_DEPTS

sales[store][month][dept] = 0;

}

8. void PrintSales( /* in */ const SalesType sales )

// Precondition:

// sales[0..NUM_STORES-1][0..NUM_MONTHS-1][0..NUM_DEPTS-1] are

// assigned

// Postcondition:

// Annual sales by department by store have been output

{

int store; // Loop control and index variable

int month; // Loop control and index variable

int dept; // Loop control and index variable

int total; // Total sales for a department

for (store = 0; store < NUM_STORES; store++)

{

// Invariant (prior to test):

// Annual sales have been output for each department

// of stores 0 through store-1

// && 0 <= store <= NUM_STORES

cout << "Store " << store;

for (dept = 0; dept < NUM_DEPTS; dept++)

{

// Invariant (prior to test):

// Annual sales have been output for departments

// 0 through dept-1 of store number "store"

// && 0 <= dept <= NUM_DEPTS

cout << " Department " << dept;

total = 0;

for (month = 0; month < NUM_MONTHS; month++)

// Invariant (prior to test):

// total == sum of values in

// sales[store][0..month-1][dept]

// && 0 <= month <= NUM_MONTHS

total = total + sales[store][month][dept];

cout << " Total Sales: " << total << endl;

}

}

}

10. There are two types of inputs to the Election program: the names of the candidates from the keyboard and the votes from file voteFile. For each candidate name, the GetNames function reads in characters until a newline is encountered or 10 characters have been read. For testing, names must be input that have fewer than 10 characters, exactly 10 characters, and more than 10 characters. File voteFile is expected to have precinct-candidate pairs, and reading continues until EOF occurs. The program must be tested with different files containing the following data combinations:

• At least one vote for each candidate for each precinct

• Exactly one vote for each candidate for each precinct

• No votes for candidate 1 in precinct 1, and so on for each candidate and each precinct

• An empty file

 

ANSWERS TO QUESTIONS

True/False

1. True

2. True

3. True

4. True

5. False

6. True

7. False

8. True

9. False

10. True

11. False

12. True

13. True

14. True

Multiple Choice

15. c

16. d

17. d

18. e

19. a

20. c

21. a

22. a

23. a

24. d

25. c

26. a

27. b

28. d

29. e

30. b

31. d

32. b

33. c

 

Fill-In

34. two-dimensional array

35. random

36. array (N-dimensional array)

37. 64

38. 56

39. 2000

40. float twoDim[10][6];

41. int twoDim[5][10];

42. Breeds twoDim[50][76];

43. int gradeBook[3][4];

44. int classScore[7][35][50];

(The dimension sizes can be in any order.)

45. 'f'

46. table[19][3] = 16.5;

47. column

48. row

49. float payRate[30][23];

50. int groupCost[8][5][100];