Chapter 4
1. relational
2. true, false
3. false, true
4. false
5. true
6. braces
7. true, false
8. default
9. if/else
10. &&
11. ||
12. !
13. left to right
14. &&
15. ||
16. block
17. cannot
18. conditional
19. integer
20. integer constant
21. break
22. 1, 0, 0, 1
23. if (y == 0)
x = 100;
24. if (y == 10)
x = 0;
else
x = 1;
25. if (sales < 10000)
commission = .10;
else if (sales <= 15000)
commission = .15;
else
commission = .20;
26. if (minimum)
hours = 10;
27. if (amount1 > 10)
if (amount2 < 100)
cout << (amount1 > amount2 ? amount1 : amount2);
28. if (grade >= 0 && grade <= 100)
cout << "The number is valid.";
29. if (temperature >= -50 && temperature <= 150)
cout << "The number is valid.";
30. if (hours < 0 || hours > 80)
cout << "The number is not valid.";
31. With character arrays you must use the following:
if (strcmp(title1, title2) < 0)
cout << title1 << " " << title2 << endl;
else
cout << title2 << " " << title1 << endl;
However, with string objects you can just use the following:
if (title1 < title2)
cout << title1 << " " << title2 << endl;
else
cout << title2 << " " << title1 << endl;
32. switch (choice)
{
case 1: cout << fixed << showpoint << setprecision(2);
break;
case 2:
case 3: cout << fixed << showpoint << setprecision(4);
break;
case 4: cout << fixed << showpoint << setprecision(6);
break;
default: cout << fixed << showpoint << setprecision(8);
break;
}
33. False
34. False
35. True
36. True
37. False
38. True
39. False
40. True
41. True
42. True
43. False
44. False
45. T, F, T, T
46. C, A, B
47. A) The first cout statement is terminated by a semicolon too early.
The definition of score1, score2, and score3 should end with a semicolon.
The following statement:
if (average = 100)
should read:
if (average == 100)
perfectScore is used before it is declared.
The following if statement should not be terminated with a semicolon:
if (perfectScore);
The conditionally-executed block in the if statement shown above should end
with a
closing brace.
B) The conditionally-executed blocks in the if/else construct should be enclosed
in braces.
The following statement:
cout << "The quotient of " << num1 <<
should read:
cout << "quotient of " << num1;
C) The trailing else statement should come at the end of the if/else construct.
D) A switch case construct cannot be used to test relational expressions.
An if/else if statement should be used instead.
48. It should read
if (!(x > 20))
49. It should use && instead of ||.
50. It should use || instead of &&.
51. The : and ? are transposed. The statement should read:
z = (a < 10) ? 0 : 7;
Chapter 5
1. increment, decrement
2. prefix
3. postfix
4. body
5. iteration
6. pretest
7. posttest loop
8. infinite or endless
9. counter
10. running total
11. accumulator
12. sentinel
13. do-while
14. while and for
15. for
16. initialization, test, update
17. nested
18. break
19. continue
20. int product = 0, num;
while (product < 100)
{
cin >> num;
product = num * 100;
}
21. do
{
float num1, num2;
char again;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Their sum is " << (num1 + num2) << endl;
cout << "Do you wish to do this again? (Y/N) ";
cin >> again;
} while (again == 'Y' || again == 'y');
22. for (int x = 0; x <= 1000; x += 10)
cout << x;
23. float total, num;
for (int count = 0; count < 10; count++)
{
cout << "Enter a number: ";
cin >> num;
total += num;
}
24. for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 15; col++)
cout << '#';
cout << endl;
}
25. int x;
do
{ cout << "Enter a number: ";
cin >> x;
} while (x > 0);
26. char sure = 'x';
while (sure != 'Y' && sure != 'N')
{ cout << "Are you sure you want quit?
cin >> sure;
}
27. for (int count = 0; count < 50; count++)
cout << "count is " << count << endl;
28. int x = 50;
while (x > 0)
{ cout << x << " seconds to go.\n";
x--;
}
29. False
30. True
31. False
32. True
33. False
34. False
35. False
36. True
37. False
38. True
39. True
40. False
41. True
42. False
43. False
44. False
45. True
46. A) The statement result = ++(num1 + num2); is invalid.
B) The while loop tests the variable again before any values are stored in it.
The while loop is missing its opening and closing braces.
C) The while statement should not end with a semicolon.
It could also be argued that BigNum should be declared a long int.
D) The variable total is not initialized to 0.
E) The expression tested by the do-while loop should be choice == 1 instead
of choice = 1.
E) The variable total is not initialized to 0.
The while loop does not change the value of count, so it iterates an infinite
number of times.
Chapter 6
1. header
2. void
3. showValue(5);
4. definition, prototype
5. arguments
6. parameters
7. value
8. prototype
9. local
10. Global
11. Global
12. 0 (for numeric variables)
13. local
14. Static
15. return
16. Default
17. last
18. constant
19. reference
20. &
21. reference
22. exit
23. parameter lists
24. True
25. False
26. True
27. True
28. False
29. True
30. False
31. True
32. False
33. True
34. True
35. True
36. True
37. False
38. True
39. True
40. False
41. False
42. False
43. A) The data type of value2 and value3 must be declared.
The function is declared void but returns a value.
B) The assignment statement should read:
average = (value1 + value2 + value3) / 3.0;
The function is declared as a float but returns no value.
C) width should have a default argument value.
The function is declared void but returns a value.
D) The parameter should be declared as:
int &value
The cin statement should read:
cin >> value;
E) The functions must have different parameter lists.
Chapter 14
1. file name
2. opened
3. close
4. fstream
5. ifstream, ofstream, fstream
6. ofstream
7. ifstream
8. fstream
9. ofstream people("people.dat");
10. ifstream pets;
11. fstream places("places.dat");
12. ofstream people("people.dat", ios::out);
people.open("people.dat", ios::out);
13. pets.open("pets.dat", ios::in);
fstream pets("pets.dat", ios::in);
14. fstream places("places.dat", ios::in | ios::out);
places.open("places.dat", ios::in | ios::out);
15. null or 0
16. fstream employees;
employees.open("emp.dat", ios::in | ios::out | ios::binary);
if (!employees)
cout << "Failed to open file.\n";
17. cout
18. eof
19. getline
20. get
21. put
22. binary
23. text, ASCII text
24. record, fields
25. structures
26. write
27. read
28. char * typecast
29. sequential
30. random
31. seekg
32. seekp
33. tellg
34. tellp
35. ios::beg
36. ios::end
37. ios::cur
38. backward
39. True
40. False
41. True
42. True
43. False
44. False
45. True
46. False
47. True
48. True
49. False
50. True
51. False
52. True
53. False
54. True
55. False
56. True
57. False
58. True
59. False
60. False
61. True
62. A) File should be opened as
fstream file("info.dat", ios::in | ios::out);
or
fstream file;
file.open("info.dat", ios::in | ios::out);
B) Should not specify ios::in with an ofstream object. Also, the if statement
should read
if (!File)
C) File access flags must be specified with fstream objects.
D) Should not write to a file opened for input. Also, the << operator
should not be used on binary files.
E) The while statement should read
while(!dataFile.eof())
F) The file access flag should be ios::in. Also, the last line should be
dataFile.getline(line, 81, '\n');
G) The get member function that takes a single parameter cannot be used to read
a string: it can only read single characters.
H) The file access flag should be ios::in. Also, the put member function cannot
be used to write a string.
I) The file access flag should be ios::out. Also, the last line should read
dataFile.write(&dt, sizeof(date));
J) The seekp member function should not be used since the file is opened for
input.