chapter 13 - cengage€¦  · web viewin a pretest loop, the loop condition is evaluated before...

49
Chapter 1: Exercise Solutions 1. a. false; b. false; c. true; d. false; e. false; f; false; g. false; h. true; i. true; j. false; k. true; l. false 3. Screen and printer. 5. An operating system monitors the overall activity of the computer and provides services. Some of these services include memory management, input/output activities, and storage management. 7. In machine language the programs are written using the binary codes while in high-level language the program are closer to the natural language. For execution, a high-level language program is translated into the machine language while a machine language need not be translated into any other language. 9. Because the computer cannot directly execute instructions written in a high-level language, a compiler is needed to translate a program written in high-level language into machine code. 11. Every computer directly understands its own machine language. Therefore, for the computer to execute a program written in a high- level language, the high-level language program must be translated into the computer’s machine language. 13. In linking an object program is combined with other programs in the library, used in the program, to create the executable code. 15. To find the weighted average of the four test scores, first you need to know each test score and its weight. Next, you multiply each test score with its weight, and then add these numbers to get the average. Therefore, 1. Get testScore1, weightTestScore1 2. Get testScore2, weightTestScore2 3. Get testScore3, weightTestScore3 4. Get testScore4, weightTestScore4 5. weightedAverage = testScore1 * weightTestScore1 + testScore2 * weightTestScore2 + testScore3 * weightTestScore3 + testScore4 * weightTestScore4; 17. To find the price per square inch, first we need to find the area of the pizza. Then we divide the price of the pizza by the area of the pizza. Let radius denote the radius and area denote the area of 1

Upload: others

Post on 18-Apr-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 1: Exercise Solutions

1. a. false; b. false; c. true; d. false; e. false; f; false; g. false; h. true; i. true; j. false; k. true; l. false

3. Screen and printer.

5. An operating system monitors the overall activity of the computer and provides services. Some of these services include memory management, input/output activities, and storage management.

7. In machine language the programs are written using the binary codes while in high-level language the program are closer to the natural language. For execution, a high-level language program is translated into the machine language while a machine language need not be translated into any other language.

9. Because the computer cannot directly execute instructions written in a high-level language, a compiler is needed to translate a program written in high-level language into machine code.

11. Every computer directly understands its own machine language. Therefore, for the computer to execute a program written in a high-level language, the high-level language program must be translated into the computer’s machine language.

13. In linking an object program is combined with other programs in the library, used in the program, to create the executable code.

15. To find the weighted average of the four test scores, first you need to know each test score and its weight. Next, you multiply each test score with its weight, and then add these numbers to get the average. Therefore,

1. Get testScore1, weightTestScore1

2. Get testScore2, weightTestScore2

3. Get testScore3, weightTestScore3

4. Get testScore4, weightTestScore4

5. weightedAverage = testScore1 * weightTestScore1 +

testScore2 * weightTestScore2 +

testScore3 * weightTestScore3 +

testScore4 * weightTestScore4;

17. To find the price per square inch, first we need to find the area of the pizza. Then we divide the price of the pizza by the area of the pizza. Let radius denote the radius and area denote the area of the circle, and price denote the price of pizza. Also, let pricePerSquareInch denote the price per square inch.

a. Get radius

b. area = π * radius * radius

c. Get price

d. pricePerSquareInch = price / area

19. To calculate the selling price of an item, we need to know the original price (the price the store pays to buy) of the item. We can then the use the following formula to find the selling price:

sellingPrice = originalPrice + originalPrice * .60

The algorithm is as follows:

a. Get originalPrice

1

Page 2: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

b. Calculate the sellingPrice using the formula:

sellingPrice = originalPrice + originalPrice * .60

The information needed to calculate the selling price is the original price and the marked-up percentage.

21. Suppose that numOfPages denotes the number of pages to be faxed and billingAmount denotes the total charges for the pages faxed. To calculate the total charges, you need to know the number of pages faxed.

If numOfPages is less than or equal to ten, the billing amount is services charges + (numOfPages × 0.20); otherwise, billing amount is service charges + 10 × 0.20 + (numOfPages - 10) × 0.10. That is,

You can now write the algorithm as follows:

a. Get numOfPages.

b. Calculate billing amount using the formula:

if (numOfPages is less than or equal to 10)

billingAmount = 3.00 + (numOfPages × 0.20);

otherwise

billingAmount = 3.00 + 10 × 0.20 + (numOfPages - 10) × 0.10;

23. Suppose averageTestScore denotes the average test score, highestScore denotes the highest test score, testScore denotes a test score, sum denote the sum of all the test scores, and count denotes the number of students in class, and studentName denotes the name of a student.

a. First you design an algorithm to find the average test score. To find the average test score, first you need to count the number of students in the class and add the test score of each student. You then divide the sum by count to find the average test score. The algorithm to find the average test score is as follows:

i. Set sum and count to 0.

ii. Repeat the following for each student in class.

1. Get testScore

2. Increment count and update the value of sum by adding the current test score to sum.

iii. Use the following formula to find the average test score.

if (count is 0)

averageTestScore = 0;

otherwise

averageTestScore = sum / count;

b. The following algorithm determines and prints the names of all the students whose test score is below the average test score.

Repeat the following for each student in class:

i. Get studentName and testScore

ii.

if (testScore is less than averageTestScore)

print studentName

2

Page 3: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

c. The following algorithm determines and highest test score

i. Get first student’s test score and call it highestTestScore.

ii. Repeat the following for each of the remaining students in class

1. Get testScore

2.

if (testScore is greater than highestTestScore)

highestTestScore = testScore;

d. To print the names of all the students whose test score is the same as the highest test score, compare the test score of each student with the highest test score and if they are equal print the name. The following algorithm accomplishes this

Repeat the following for each student in class:

i. Get studentName and testScore

ii.

if (testScore is equal to highestTestScore)

print studentName

You can use the solutions of the subproblems obtained in parts a to d to design the main algorithm as follows:

1. Use the algorithm in part a to find the average test score.

2. Use the algorithm in part b to print the names of all the students whose score is below the average test score.

3. Use the algorithm in part c to find the highest test score.

4. Use the algorithm in part d to print the names of all the students whose test score is the same as the highest test score

3

Page 4: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 2 : Exercises Solutions

1. a. false; b. false; c. false; d. true; e. true; f. false; g. true; h. true; i. false; j. true; k. false

3. b, d, e

5. The identifiers firstName and FirstName are not the same. C++ is case sensitive. The first letter of firstName is lowercase f while the first character of FirstName is uppercase F. So these identifiers are different.

7. a. 3

b. Not possible. Both the operands of the operator % must be integers. Because the second operand, w, is a floating-point value, the expression is invalid.

c. Not possible. Both the operands of the operator % must be integers. Because the first operand, which is y + w, is a floating-point value, the expression is invalid .

d. 38.5

e. 1

f. 2

g. 2

h. 420.0

9. 7

11. a and c are valid

13. a. 32 * a + b

b. '8'

c. "Julie Nelson"

d. (b * b – 4 * a * c) / (2 * a)

e. (a + b) / c * (e * f) – g * h

f. (–b + (b * b – 4 * a * c)) / (2 * a)

15. x = 20

y = 15

z = 6

w = 11.5

t = 4.5

17. a. 0.50

b. 24.50

c. 37.6

d. 8.3

e. 10

4

Page 5: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

f. 38.75

19. a and c are correct

21. a. int num1;

int num2;

b. cout << "Enter two numbers separated by spaces." << endl;

c. cin >> num1 >> num2;

d. cout << "num1 = " << num1 << "num2 = " << num2

<< "2 * num1 – num2 = " << 2 * num1 – num2 << endl;

23. A correct answer is:

#include <iostream>

using namespace std;

const char STAR = '*';const int PRIME = 71;

int main(){ int count, sum; double x;

int newNum; //declare newNum

count = 1; sum = count + PRIME; x = 25.67; // x = 25.67; newNum = count * 1 + 2; //newNum = count * ONE + 2; sum = sum + count; //sum + count = sum; x = x + sum * count; // x = x + sum * COUNT; cout << " count = " << count << ", sum = " << sum << ", PRIME = " << PRIME << endl; return 0;}

25. An identifier must be declared before it can be used.

27. a. x *= 2;

b. x += y - 2;

c. sum += num;

d. z *= x + 2;

e. y /= x + 5;

29.

a b c

a = (b++) + 3; 9 7 und

c = 2 * a + (++b); 9 8 26

b = 2 * (++c) – (a++); 10 45 27

5

Page 6: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

31. (The user input is shaded.)

a = 25Enter two integers: 20 15

The numbers you entered are 20 and 15z = 45.5Your grade is AThe value of a = 65

33.

#include <iostream> #include <string>

using namespace std;

const double X = 13.45;const int Y = 34;const char BLANK = ' ';

int main() { string firstName, lastName; int num; double salary;

cout << "Enter first name: "; cin >> firstName; cout << endl;

cout << "Enter last name: "; cin >> lastName; cout << endl;

cout << "Enter a positive integer less than 70: "; cin >> num; cout << endl; salary = num * X; cout << "Name: " << firstName << BLANK << lastName << endl; cout << "Wages: $" << salary << endl; cout << "X = " << X << endl; cout << "X + Y = " << X + Y << endl; return 0; }

6

Page 7: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 3

1. a. true; b. true; c. false; d. false; e. true; f. true

3. a. x = 37, y = 86, z = 0.56

b. x = 37, y = 32, z = 86.56

c. Input failure: z = 37.0, x = 86, trying to read the . (period) into y.

5. Input failure: Trying to read A into y, which is an int variable. x = 46, y = 18, and z = 'A'. The values of y and z are unchanged.

7. iomanip

9. getline(cin, name);

11. a. name = " Lance Grant", age = 23

b. name = " ", age = 23

13.#include <iostream>#include <fstream>

using namespace std;

int main(){ int num1, num2; ifstream infile; ofstream outfile;

infile.open("input.dat"); outfile.open("output.dat");

infile >> num1 >> num2; outfile << "Sum = " << num1 + num2 << endl;

infile.close(); outfile.close();

return 0;}

15. fstream

17. a. Same as before.

b. The file contains the output produced by the program.

c. The file contains the output produced by the program. The old contents are erased.

d. The program would prepare the file and store the output in the file.

19. a. outfile.open("travel.dat ");b. outfile >> fixed >> showpoint >> setprecision(2);c. outfile >> day >> " " >> distance >> " " >> speed >> endl;d. travelTime = distance / speed; outfile >> travelTime;e. fstream and iomanip.

7

Page 8: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 4

1. a. false; b. false; c. false; d. true; e. false; f. false; g. false; h. false; i. false; j. true

3. a. true; b. false; c. true; d. true; e. false

5. a. x = y: 0

b. x != z: 1

c. y == z – 3: 1

d. !(z > w): 0

e. x + y < z: 0

7. Omit the semicolon after else. The correct statement is:

if (score >= 60) cout << "You pass." << endl;else cout << "You fail." << endl;

9. 3 110. a. 2 2

b. 3 1

11. if (sale > 20000) bonus = 0.10else if (sale > 10000 && sale <= 20000) bonus = 0.05;else bonus = 0.0;

13. a. The output is: Discount = 10%. The semicolon at the end of the if statement terminates the if statement. So the cout statement is not part of the if statement. The cout statement will execute regardless of whether the expression in the if statement evaluates to true or false.

b. The output is: Discount = 10%. The semicolon at the end of the if statement terminates the if statement. So the cout statement is not part of the if statement. The cout statement will execute regardless of whether the expression in the if statement evaluates to true or false.

15. a. (x >= y) ? z = x – y : z = y – x;

b. (hours >= 40.0) ? wages = 40 * 7.50 + 1.5 * 7.5 * (hours – 40) : wages = hours * 7.50;

c. (score >= 60) ? str = "Pass" : str = "Fail";

17. a. 40.00b. 40.00c. 55.00

19. 15

21. 96

23. #include <iostream>

using namespace std;

8

Page 9: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

const int SECRET = 5;

int main(){ int x, y, w, z;

z = 9;

if (z > 10) { x = 12; y = 5; w = x + y + SECRET; } else { x = 12; y = 4; w = x + y + SECRET; }

cout << "w = " << w << endl;

return 0;}

9

Page 10: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

10

Page 11: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 5

1. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false

3. 5

5. if ch > 'Z' or ch < 'A'

7. Sum = 158

9. Sum = 158

11. 11 18 25

13. Replace the while loop statement with the following:

while (response == 'Y' || response == 'y')

Replace the cout statement:

cout << num1 << " + " << num2 << " = " << (num1 - num2) << endl;

with the following:

cout << num1 << " + " << num2 << " = " << (num1 + num2) << endl;15. 4 3 2 1

17. 0 3 8 15 24

19. Loop control variable: j

The initialization statement: j = 1;

Loop condition: j <= 10;

Update statement: j++

The statement that updates the value of s: s = s + j * (j – 1);

21. 2 7 17 37 77 157 317

23. a. *

b. infinite loop

c. infinite loop

d. ****

e. ******

f. ***

25. The relationship between x and y is: 3y = x.

Output: x = 19683, y = 10

27.

0 - 2425 - 4950 - 7475 - 99100 - 124

11

Page 12: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

125 - 149150 - 174175 - 200

29. a. both

b. do...while

c. while

d. while

31. In a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated after executing the body of the loop. A posttest loop executes at least once, while a pretest loop may not execute at all.

33. int num;

do

{ cout << "Enter a number less than 20 or greater than 75: "; cin >> num; } while (20 <= num && num <= 75);

35. int i = 0, value = 0;

do { if (i % 2 == 0 && i <= 10) value = value + i * i; else if (i % 2 == 0 && i > 10) value = value + i; else value = value - i; i = i + 1; } while (i <= 20);

cout << "value = " << value << endl;

The output is: value = 200

37 cin >> number;while (number != -1){ total = total + number; cin >> number;}cout << endl;cout << total << endl;

39. a. number = 1;while (number <= 10){ cout << setw(3) << number; number++;}

12

Page 13: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

b.number = 1;do{ cout << setw(3) << number; number++;} while (number <= 10);

41. 11 18 25

43 -1 0 3 8 15 24

45. 12 11 9 7 6 4 2 1

13

Page 14: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 6

1. a. false; b. true; c. true; d. true; e. false3. a. 4 b. 10.8 c. 2.5 d. 10.24 e. 15.625

f. 5 g. 2.5 h. 9 i. 28 j. 36

5. (iii)7. a, b, c, e, f, and g are valid. In d, the function call in the output (cout) statement requires one more

argument.

9. a. 4; int

b. 2; double

c. 4; char

d. The function test requires four actual parameters. The order of the parameters is: int, char, double, int.

e. cout << test(5, 'z', 7.3, 5) << endl;

f. cout << two(17.5, 18.3) << endl;

g. cout << static_cast<char>(static_cast<int>(three(4, 3, 'A', 17.6)) + 1)

<< endl;

11. bool isUppercaseLetter(char ch) { if (isupper(ch)) return true; else return false; }13. a. (i) 125 (ii) 432

b. The function computes x3, where x is the argument of the function.

15. 1 4 9 16 25 36 49 64 81 100

17. double funcEx17(double x, double y, double z) { return (x + y) * z; }

14

Page 15: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 7

1. a. true; b. false; c. true; d. false; e. true; f. false; g. false; h. false; i. true

3. a. A variable declared in the heading of a function definition is called a formal parameter. A variable or expression used in a function call is called an actual parameter.

b. A value parameter receives a copy of the actual parameter’s data. A reference parameter receives the address of the actual parameter.

c. A variable declared within a function or block is called a local variable. A variable declared outside of every function definition is called a global variable.

5. void funcThreeTimes(double x){ cout << fixed << showpoint << setprecision(2); cout << 3 * x << endl;}

7. void initialize(int& x, double& y, string& str){ x = 0; y = 0; str = "";}

9. 5, 10, 1520, 10, 1525, 30, 1545, 30, 60

11. #include <iostream>

using namespace std;

void func(int val1, int val2);

int main() { int num1, num2;__1__ cout << "Please enter two integers." << endl;__2__ cin >> num1 >> num2;__3__ func (num1, num2);__7__ cout << " The two integers are " << num1 << ", " << num2 << endl;__8__ return 0; }

void func (int val1, int val2) { int val3, val4;__4__ val3 = val1 + val2;__5__ val4 = val1 * val2;__6__ cout << "The sum and product are " << val3 << " and " << val4 << endl; }

13. void traceMe(double& x, double y, double& z){

15

Page 16: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

if (x != 0) z = sqrt(y) / x; else {

cout << "Enter a nonzero number: "; cin >> x; cout << endl; z = floor(pow(y, x)); }

}

15. 10 205 20

17. 11, 316, 219, 324, 2

19. (a), (b), and (d) are correct.

16

Page 17: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 8

1. a. true; b. false; c. true; d. false; e. false; f. true; g. true; h. true; i. false; j. false; k. false

3. Only a and c are valid.

5. The statement:

using namespace std;

is missing between Lines 1 and 2.

7. Either include the statement:

using namespace aaa;

before the function main or refer to the identifiers x and y in main as aaa::x and aaa::y, respectively.

9. a. Hello --> Jello

b. Bingo --> Ringo

c. Sunny --> Bunny

11. Going to the Amusement Park

1410musemABCDEFGHIJK11aBdDEFGHIJK

17

Page 18: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 91. a. true; b. true; c. false; d. false; e. true; f. false; g. false; h. false; i. true; j.

false; k. false; l. false

3. a. This declaration is correct.

b. The const declaration should be: const int SIZE = 100;double list[SIZE];

c. This declaration should be: int numList[10];d. This declaration is correct.e. This declaration should be: double scores[50];5. 0 to 49

7. -3 -1 1 3 55 -1 8 3 -1

9. 5 6 9 19 23 37

11. If array index is less than 0 or greater than arraySize – 1, we say that the

array index is out-of bound. C++ does not check for array indices within

bound.

13. a. double heights[10] = {5.2, 6.3, 5.8, 4.9, 5.2, 5.7, 6.7, 7.1, 5.10, 6.0};ordouble heights[] = {5.2, 6.3, 5.8, 4.9, 5.2, 5.7, 6.7, 7.1, 5.10, 6.0};

b. int weights[7] = {120, 125, 137, 140, 150, 180, 210};orint weights[] = {120, 125, 137, 140, 150, 180, 210};

c. char specialSymbols[] = {'$', '#', '%', '@', '&', '! ', '^'};.d. string seasons[4] = {"fall", "winter", "spring", "summer"};

orstring seasons[] = {"fall", "winter", "spring", "summer"};

15. list[0] = 8, list[1] = 9, list[2] = 15, list[3] = 12, list[4] = 80, list[5] = 0, list[6] = 0, list[7] = 0, list[8] = 0, and list[9] = 0.

17. a. Correct.

b. Correct.

18

Page 19: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

c. Incorrect. The size of score is 50, so the call should be tryMe(score, 50);d. Correct.e. Incorrect. The array gpa is of type double while the parameter x of tryMe is of

type int. So there will be mismatch data type error.

19. 1 25000.00 750.002 36500.00 1095.003 85000.00 2550.004 62500.00 1875.005 97000.00 2910.00

21. List elements: 11 16 21 26 30

23. 1 3.50 10.70 235.312 7.20 6.50 294.053 10.50 12.00 791.684 9.80 10.50 646.545 6.50 8.00 326.73

25. No.

27. a. Invalid; the assignment operator is not defined for C-strings.b. Invalid; the relational operators are not defined for C-strings.c. Invalid; the assignment operator is not defined for C-strings.d. Valid

29. a. strcpy(str1, "Sunny Day");b. length = strlen(str1);c. strcpy(str2, name);d. if (strcmp(str1, str2) <= 0)

cout << str1 << endl;else cout << str2 << endl;

31. int temp[3][4] = {{6, 8, 12, 9}, {7, 5, 10, 6}, {4, 13, 16, 20}};

33.a. 30 b. 5 c. 6 d. row e. column

35. a. beta is initialized to 0.

b. First row of beta: 0 1 2Second row of beta: 1 2 3Third row of beta: 2 3 4

c. First row of beta: 0 0 0Second row of beta: 0 1 2Third row of beta: 0 2 4

19

Page 20: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

d. First row of beta: 0 2 0Second row of beta: 2 0 2Third row of beta: 0 2 0

20

Page 21: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 10

1. a. false; b. true; c. false; d. false; e. false

3. a.

int seqOrderedSearch(const int list[], int listLength, int searchItem){ int loc; bool found = false;

for (loc = 0; loc < listLength; loc++) if (list[loc] >= searchItem) { found = true; break; } if (found) if (list[loc] == searchItem) return loc; else return -1; else return -1;

}

b. i. 5 ii. 7 iii. 8 iv. 11

5. List before the first iteration: 26, 45, 17, 65, 33, 55, 12, 18

List after the first iteration: 26, 17, 45, 33, 55, 12, 18, 65

List after the second iteration: 17, 26, 33, 45, 12, 18, 55, 65

List after the third iteration: 17, 26, 33, 12, 18, 45, 55, 65

List after the fourth iteration: 17, 26, 12, 18, 33, 45, 55, 65

List after the fifth iteration: 17, 12, 18, 26, 33, 45, 55, 65

List after the sixth iteration: 12, 17, 18, 26, 33, 45, 55, 65

List after the seventh iteration: 12, 17, 18, 26, 33, 45, 55, 65

7. 3

9. 10, 12, 18, 21, 25, 28, 30, 71, 32, 58, 15

11. Bubble sort: 49,995,000; selection sort: 49,995,000; insertion sort: 25,007,499

13. 26

15. To use a vector object in a program, the program must include the header file vector.

17. 1 3 5 7 9

19.

a. vector<int> secretList;b.

21

Page 22: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

secretList.push_back(56);secretList.push_back(28);secretList.push_back(32);secretList.push_back(96);secretList.push_back(75);

c.for (unsigned int i = 0; i < secretList.size(); i++) cout << secretList[i] << " ";

cout << endl;

21. a. cout << myList.front() << " " << myList.back() << endl;

b length = myList.size();

c.for (int i = 0; i < myList.size(); i++) cout << myList[i] << " ";

cout << endl;

22

Page 23: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 11

1. a. false; b. false; c. true; d. true; e. true; f. true; g. false

3. checkingAccount newAcct;

newAcct.name = "Jason Miller";newAcct.accountNum = 17328910;newAcct.balance = 24476.38;newAcct.interestRate = 0.025;

5. movieType newRelease;

newRelease.name = "Summer Vacation";newRelease.director = "Tom Blair";newRelease.producer = "Rajiv Merchant";newRelease.yearReleased = 2005;newRelease.copiesInStock = 34;

7. a. Invalid; the member name of newEmployee is a struct. Specify the member names to store the value "John Smith". For example,

newEmployee.name.first = "John";newEmployee.name.last = "Smith";

b. Invalid; the member name of newEmployee is a struct. There are no aggregate output operations on a struct. A correct statement is:

cout << newEmployee.name.first << " " << newEmployee.name.last << endl;

c. Valid

d. Valid

e. Invalid; employees is an array. There are no aggregate assignment operations on arrays.

9. partsType inventory[100];

11. void getData(partsType& pType){ for (int j = 0; j < length; j++) { cin >> pType.partName; cin >> pType.partNum; cin >> pType.price; cin >> pType.quantitiesInStock; }}

for (int j = 0; j < 100; j++) getData(inventory[i]);

23

Page 24: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 12

1. a. false; b. false; c. true; d. false; e. false;

3. a. 6

b. 2

c. 2

d.

void xClass::func(){ u = 10; w = 15.3;}

e. void xClass::print(){ cout << u << " " << w << endl;}

f. xClass::xClass(){ u = 0; w = 0;}

g. x.print();

h. xClass t(20, 35.0);

5. a. int testClass::sum(){ return x + y;}

void testClass::print() const{ cout << "x = " << x << ", y = " << y << endl;}

testClass::testClass(){ x = 0; y = 0;}

testClass::testClass(int a, int b){ x = a; y = b;}

b. One possible solution. (We assume that the name of the header file containing the definition of the class testClass is Exercise5Ch12.h.)

#include <iostream>

24

Page 25: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

#include "Exercise5Ch12.h"

int main(){ testClass one; testClass two(4, 5);

one.print(); two.print();

return 0;}

7. a. personType student("Buddy", "Arora");

b. student.print();

c. student.setName("Susan", "Gilbert");

9. A constructor is a member of a class and it executes automatically when a

class object is instantiated and a call to the constructor is specified in the

object declaration. A constructor is included in a class so that the objects are

properly initialized when they are declared.

11. A destructor is a member of a class and if it is included in a class, it executes

automatically when a class object goes out of scope. Its main purpose is to

deallocate the dynamic memory created by an object.

13. a. myClass::count = 0;

b. myClass.incrementCount();c. myClass.printCount();d.

int myClass::count = 0;

void myClass::setX(int a){ x = a;}

void myClass::printX() const{ cout << x;}

void myClass::printCount()

25

Page 26: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

{ cout << count;}

void myClass::incrementCount(){ count++;}

myClass::myClass(int a){ x = a;}

e. myClass myObject1(5);f. myClass myObject2(7);g.

The statements in Lines 1 and 2 are valid.

The statement in Line 3 should be: myClass::printCount();.

The statement in Line 4 is invalid because the member function printX is not a static member of the class, so cannot be called by using the name of class.

The statement in Line 5 is invalid because count is a private static member variable of the class.

h. 52231433

26

Page 27: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 13

1. a. true; b. true; c. true

3. Some of the member variables that can be added to the class

employeeType are: department, salary, employeeCategory (such as

supervisor and president), and employeeID. Some of the member functions

are: setInfo, getSalary, getEmployeeCategory, and setSalary.

5. The private members of the object newCylinder are radius and height.

7. a. The statement :

class bClass public aClass

should be:

class bClass: public aClass

Also missing semicolon after }.

b. Missing semicolon after the statement cClass(int), and missing semicolon after }.

9. a.

yClass::yClass(){ a = 0; b = 0;}

b.xClass::xClass(){ z = 0;}

c.void yClass::two(int u, int v){ a = u; b = v;}

11.The protected members of a base class can be directly accessed by the

member functions of the derived class, but they cannot be directly accessed

27

Page 28: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

in a program that uses that class. The public members of a class can be

directly accessed by the member functions of any derived class as well as in

a program that uses that class.

13. The members setX, print, y, and setY are protected members in class third.

The private member x of class first is hidden in class third and it can be

accessed in class third only through the protected and public members of

class first.

15. Because the memberAccessSpecifier is not specified, it is a private

inheritance. Therefore, all the members of the class first becomes private

members in class fifth.

17.a. void two::setData(int a, int b, int c){ one::setData(a, b); z = c;}

b. void two::print() const{ one::print(); cout << z << endl;}

19. In base: x = 7In derived: x = 3, y = 8; x + y = 11****7####11

28

Page 29: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 14

1. a. false; b. false; c. false; d. true; e. true; f. true; g false; h. false

3. The operator * is used to declare a pointer variable and to access the

memory space to which a pointer variable points.

5. 98 9898 98

7.b and c

9. 78 7811. 27 35

73 2736 36

13.4 4 5 7 10 14 19 25 32 4015. The operator delete deallocates the memory space to which a pointer points.

17. a. num = new int[10]; b. for (int j = 0; j < 10; j++)

cin >> num[j];

c. delete [] num;19.In a shallow copy of data, two or more pointers point to the same memory

space. In a deep copy of data, each pointer has its own copy of the data.21.Array p: 5 7 11 17 25

Array q: 25 17 11 7 5

23. The copy constructor makes a copy of the actual variable.25.Classes with pointer data members should include the destructor, overload

the assignment operator, and explicitly provide the copy constructor by including it in the class definition and providing its definition.

27.ClassA x: 4

ClassA x: 6ClassB y: 10

29. Yes.31. a. Because employeeType is an abstract class, you cannot instantiate an object of

this class. Therefore, this statement is illegal.b. This statement is legal.c. This statement is legal.

29

Page 30: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 15 

1. a. false; b. true; c. true; d. false; e. false; f. true; g. false; h. true; i. false; j. true; k. false

3. A friend function is nonmember of a class while a member function is a member of a class.

5. Because the left operand of << is a stream object, which is not of the type mystery.

7. When the class has pointer data members.

9. a. friend strange operator+(const strange&, const strange&);

b. friend bool operator==(const strange&, const strange&);

c. friend strange operator++(strange&, int);

11. In Line 2, the word friend before the word bool is missing. The correct statement is:

friend bool operator<=(mystery, mystery); //Line 2

13. Because leftmost operand of << is not an object of the class type for which << is overloaded.

15. A reference to an object of the class istream.

17. Suppose that a class, say temp, overloads the pre- and post-increment operator ++ and tempObj is an object of the class temp. Then the statement ++tempObj; is compiled as tempObj.operator++(); ; and the statement tempObj++; is compiled as tempObj.operator++(0);. The dummy parameter distinguishes between the pre- and post-increment operator functions. Similar conventions for the pre- and post-decrement operators.

19. None.

21. One.

23.

class complexType{ //overload the stream insertion and extraction operators friend ostream& operator<<(ostream&, const complexType&); friend istream& operator>>(istream&, complexType&);

public: void setComplex(const double& real, const double& imag); //set the complex number according to the parameters //Postcondition: realPart = real; imaginaryPart = imag

complexType(double real = 0, double imag = 0); //constructor //initialize the complex number according to the parameters //Postcondition: realPart = real; imaginaryPart = imag

complexType operator+(const complexType& otherComplex) const;

30

Page 31: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

//overload + complexType operator*(const complexType& otherComplex) const; //overload *

complexType operator~() const;

double operator!() const;

bool operator==(const complexType& otherComplex) const; //overload ==

private: double realPart; //variable to store the real part double imaginaryPart; //variable to store the imaginary part};

// Definitions of operator~ and operator!complexType complexType::operator~() const{ complexType temp = *this;

temp.imaginaryPart = -temp.imaginaryPart;

return temp;}

double complexType::operator!() const{ return (pow((realPart * realPart + imaginaryPart * imaginaryPart), 0.5));}

25. Error in Line 4. A template instantiation can be for only a built-in type or a user-defined type. The word “type” between the angular brackets must be replaced either with a built-in type or a user-defined type.

27. a. 12 b. Sunny Day

29.

template <class Type>void swap(Type &x, Type &y){ Type temp; temp = x; x = y; y = temp;}

31

Page 32: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 16

1. a. false, b. true, c. true, d. false

3. The program will terminate with an error message.

5. If an exception is thrown in a try block, the remaining statements in that try

block are ignored. The program searches the catch blocks in the order they

appear after the try block, and looks for an appropriate exception handler. If

the type of thrown exception matches the parameter type in one of the catch

blocks, the code of that catch block executes and the remaining catch blocks

after this catch block are ignored.

7. a. Leaving the try block.b. Current balance: 975 Balance must be greater than 1000.00c. Current balance: -2000 Balance must be greater than 1000.00

9. a.

Entering the try block.Exception: Lower limit violation.After the catch block

b.Entering the try block.Exception: 0After the catch block

c. Entering the try block.Exiting the try block.After the catch block

d.Entering the try block.Exception: 0After the catch block

11. A throw statement.

13. (Assume that the definition of the class tornadoException is in the header

file tornadoException.h.)

32

Page 33: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

#include <iostream>#include "tornadoException.h"

using namespace std;

int main(){ int miles;

try { cout << "Enter the miles: "; cin >> miles; cout << endl;

if (miles < 5) throw tornadoException(); else throw tornadoException(miles); } catch (tornadoException tE) { cout << tE.what() << endl; }

return 0;}

15. A function specifies the exceptions it throws in its heading using the throw

clause.

33

Page 34: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 17

1. a. true; b. true; c. false; d. false; e. false

3. The case in which the solution is defined in terms of smaller versions of itself.

5. A function that calls another function and eventually results in the original function call is said to be indirectly recursive.

7. a. The statements in Lines 2 and 3.

b. The statements in Lines 4 and 5.

c. Any nonnegative integer.

d. It is a valid call. The value of mystery(0) is 0.

e. It is a valid call. The value of mystery(5) is 15.

f. It is an invalid call. It will result in infinite recursion.

9. a. 2 b. 7 c. 3 d. -855 68

11. a. It does not produce any output.

b. 5 6 7 8 9

c. It does not produce any output.

d. It does not produce any output.

13. a. 2; b. 3; c. 5; d. 21

15.

The base cases are when n = 0 or n = 1. The general case is specified by the option otherwise.

34

Page 35: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 18

1. a. false; b. false; c. false; d. false

3. NULL

5. Before deletion, the link field of the third node stores the address of the fourth node. After deletion the link field of the third node will store the address of the next node, which is the old fifth node. If there was no fifth node, after deletion, the link field will store the value NULL. Therefore after deleting the fourth node, the link field of the third node is changed. So a pointer to the third node is needed.

7. a. true

b. true

c. false

d. false

e. true

9. a. A = A->link;

b. list = A->link->link;

c. B = B->link->link;

d. list = NULL;

e. B->link->info = 35;

f. newNode = new nodeType;

newNode->info = 10;

newNode->link = A->link;

A->link = newNode;

g. p = A->link;

A->link = p->link;

delete p;

11. This is an invalid code. The statement s->info = B; is invalid because B is a pointer and s->info is an int.

13. 10 18 13

15. nodeType head, p, q;

head = new nodeType;head->info = 72;head->link = NULL;p = new nodeType;p->info = 43;p->link = head;head = p;p = head->link;q = new nodeType;q->info = 8;q->link = NULL;p->link = q;

35

Page 36: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

q = new nodeType;q->info = 12;q->link = p;head->link = q;

p = head;while (p != NULL){ cout << p->info << " "; p = p->link;}cout << endl;

The output of this code is: 43 12 72 8

17. Item to be deleted is not in the list.18 38 2 15 45 25

19.

36

Page 37: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

21.

37

Page 38: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

Chapter 19

1. The two basic operations on a stack are push, to add an element to a stack, and pop, to remove an element from a stack.

3. x = 3y = 9713 4 7

5. a. 26

b. 45

c. 8

d. 29

7. a. A * B + Cb. (A + B) * (C – D)c. (A – B – C) * D

9. 10 20 30 40 50

11. If the stack is nonempty, the statement stack.top(); returns the top element of the stack and the statement stack.pop(); removes the top element of the stack

13. template <class elemType>elemType second(stackType<elemType> stack){ elemType temp1, temp2;

if (stack.isEmptyStack()) { cout << "Stack is empty." << endl; exit(0); //terminate the program }

temp1 = stack.top(); stack.pop();

if (stack.isEmptyStack()) { cout << "Stack has only one element." << endl; exit(0); //terminate the program } temp2 = stack.top(); stack.push(temp1);

return temp2;

38

Page 39: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

}

15. Queue Element = 0Queue Element = 14Queue Element = 22Sorry, the queue is emptySorry, the queue is emptyStack Element = 32Stack Elements: 64 28 0Queue Elements: 30

17. a. queueFront = 50; queueRear = 0.b. queueFront = 51; queueRear = 99.

19. a. queueFront = 25; queueRear = 76.b. queueFront = 26; queueRear = 75.

21. 51

23. template <class Type>void reverseStack(stackType<Type> &s){ linkedQueueType<Type> q; Type elem;

while (!s.isEmptyStack()) { elem = s.top(); s.pop(); q.addQueue(elem); }

while (!q.isEmptyQueue()) { elem = q.front(); q.deleteQueue(); s.push(elem); }}

25. template <class Type>int queueType<Type>::queueCount(){ return count;}

39

Page 40: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

27.

40

Page 41: Chapter 13 - Cengage€¦  · Web viewIn a pretest loop, the loop condition is evaluated before executing the body of the loop. In a posttest loop, the loop condition is evaluated

29.

41