• No results found

If you add the title to the ComparedTo function, the titles would be compared as strings Mr would come before Mrs and Dr would come before Mr There would be no semantic

In document Answers to Selected Exercises (Page 80-85)

sense in such a comparison.

Chapter 12

Exam Preparation Exercises

1.

False.

2.

True.

3.

False.

4.

True.

5.

True.

6.

C++ does not report an out-of-bounds access as an error. The program accesses the loca-

tion preceding the first element of the array, which may be a completely unrelated value

in memory.

7.

Arrays can be passed as arguments to reference parameters. No other aggregate opera-

tions are allowed.

8.

The base address is the memory location of the first element of an array. It is the value

that is passed to a reference parameter in a function call.

9.

A string.

10.

The inner loop would increment the column index, keeping the row constant. The outer

loop would increment the row index to move to the next row after each row is processed.

11.

a. 27, b. 15, c. 100,000,000.

12.

The For loop runs from 1 to 100 instead of 0 to 99. The last iteration will assign zero to

an out-of-bounds location.

13.

Comparison of arrays is not allowed in C++.

14.

Functions cannot return array types.

15.

The rows and columns are reversed in the initializer list. This list fits an array that is

[4][3]

. However, C++ would still place the 12 values within the array—just not in the

locations that we would expect by looking at their arrangement in the code.

16.

Because the index value is input and then used without any checking, an out-of-bounds

array access is likely.

17.

In addition to returning the maximum value, the function returns with the array ordered

least to greatest.

18.

* ** * * * ** * * * **

19.

***

20.

* * * * * * * *

* * * * *

21. a.

for (int col = 0; col < 15; col++)

cout << examPrep[0][col] << " ";

b.

for (int row = 0; row < 15; row++)

cout << examPrep[row][0] << " ";

c.

for (int row = 0; row < 7; row++)

{

for (int col = 0; col < 15; col++) cout << examPrep[row][col] << " "; cout << endl;

}

d.

for (int row = 11; row >= 0; row--) {

for (int col = 14; col >= 0; col--) cout << examPrep[row][col] << " "; cout << endl;

}

Chapter 12

Programming Warm-Up Exercises

1. a.

string topTenList[10];

b.

enum Spectrum {RED, ORANGE, YELLOW, BLUE, GREEN, INDIGO, VIOLET}; float colorMix[7];

c.

struct dayRecord { int Day; string activity; };

enum DayNames {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};

dayRecord Calendar[6][7];

2.

typedef float DataSet[5];

DataSet input; DataSet output; DataSet working;

3.

DataSet set[3];

4.

for (int row = 0; row < 3; row++) for (int col = 0; col < 5; col++)

set[row][col] = 0.0;

5.

bool Equals (/* in */ const DataSet first, /* in */ const DataSet second)

6.

bool Equals (/* in */ const DataSet first,

/* in */ const DataSet second) {

for(int index = 0; index < 5; index++) if (first[index] != second[index])

return false;

// Only reaches end of loop if all are equal return true; }

7.

struct Size { int height; int width; };

enum Medium {OIL, WATERCOLOR, PASTEL, ACRYLIC, PRINT, COLORPHOTO, BWPHOTO}; enum Room {MAIN, GREEN, BLUE, NORTH, SOUTH,

ENTRY, BALCONY}; struct ArtWork { string artist; string title; Medium medium; Size size; Room room; float price; }

typedef ArtWork GalleryList[120]; ArtWork currentList; int numPieces;

8. a.

currentList[36]

b.

currentList[11].title

c.

currentList[84].size.width

d.

currentList[119].room

e.

currentList[77].artist[0]

9.

for (int index = 0; index < numPieces; index++) cout << currentList[index].artist << " "

<< currentList[index].title << " " << currentList[index].price << endl;

10.

float total = 0.0;

for (int index = 0; index < numPieces; index++) total = total + currentList[index].price;

11.

for (int index = 0; index < numPieces; index++)

if (currentList[index].room == BLUE)

cout << currentList[index].title << endl;

12.

float total = 0.0;

for (int index = 0; index < numPieces; index++) if(currentList[index].medium == OIL)

if (currentList[index].size.width * currentList[index].size.height > 400) total = total + currentList[index].price;

13.

enum Notes {C, CSHARP, D, DSHARP, E, F, FSHARP,

G, GSHARP, A, ASHARP, B}; float scale[8][12];

14.

for (int octave = 0; octave < 8; octave++)

for (Notes note = A; note <= GSHARP; note = Notes(note + 1)) cin >> scale[octave][note];

15.

for (Notes note = A; note <= GSHARP; note = Notes(note + 1)) cout << scale[3][note] << endl;

16.

for (int octave = 0; octave < 8; octave++) cout << scale[octave][C] << endl;

17.

float humidity[10][52][50][3];

18.

void Reset (float humidity[10][52][50][3]) {

for (int year = 0; year < 10; year++) for (int week = 0; week < 52; week++)

for (int state = 0; state < 50; state++) for (Type type = MAX; type <= AVERAGE;

type = Type(type + 1)); humidity = 0.0; }

19.

struct TimePlace { int year; int week; int state;

float difference; }

TimePlace MaxSpread(float humidity[10][52][50][3]) { TimePlace max; float difference; max.year = 0; max.week = 0; max.state = 0; max.difference = 0.0;

for (int year = 0; year < 10; year++) for (int week = 0; week < 52; week++)

for (int state = 0; state < 50; state++) { difference = humidity[year][week][state][MAX] – humidity[year][week][state][MIN]; if (difference > max.difference) { max.difference = difference; max.year = year; max.week = week; max.state = state; } } return max; }

20.

for (int year = 5; year < 10; year++) for (int week = 0; week < 52; week++)

cout << humidity[year][week][22][AVERAGE] << endl;

Chapter 12

Case Study Follow-Up Exercises

1.

The input file cannot be found, a grade is outside the range of 0 to 100, a grade is nega-

tive.

2.

It would be more efficient to calculate and print the values without storing them. How-

ever, the Print Results module would have two major tasks: calculating values and print-

ing them. It is better style to separate these tasks.

3.

When an index has semantic content, the index’s role in the problem is more than just as

an accessor into an array. The index has meaning within the problem itself. In this

problem, a grade was used as an index into the grade’s frequency counter. If grade is 60,

grades[grade] tells how many times the grade 60 has occurred in the file.

4.

void OpenFiles(ifstream& text, ofstream& outFile)

// Function OpenFiles reads in the names of the input file and the // output file, opens them for processing, prompts for a heading // for the output file, reads the heading, and prints

// it in the file {

string inFileName; string outFileName; string heading;

cout << "Enter the name of the file to be processed" << endl; cin >> inFileName;

text.open(inFileName.c_str());

cout << "Enter the name of the output file" << endl; cin >> outFileName;

outFile.open(outFileName.c_str());

cout << "Enter a heading for the output file." << endl; cin >> heading;

outFile << heading << endl << endl; }

5.

It might be slightly better to have the input of the heading in the function that does the

printing.

Chapter 13

Exam Preparation Exercises

1.

Because each component except the first has a unique predecessor, and each component

except the last has a unique successor.

2.

That all of the components have the same type.

3. a.20, b.42, c.42, d.1.

4.

while (index < length && fabs(item –data[index]) >= EPSILON)

We need to change it because floating point numbers cannot be compared reliably for

In document Answers to Selected Exercises (Page 80-85)