1
1
# Chapter05: Statements
2
- ## [ Exercise 5.1] ( Chapter05/ 5.01.txt)
2
+ ## [ Exercise 5.1] ( 5.01.txt )
3
3
What is a null statement? When might you use a null statement?
4
- ## [ Exercise 5.2] ( Chapter05/ 5.02.txt)
4
+ ## [ Exercise 5.2] ( 5.02.txt )
5
5
What is a block? When might you might use a block?
6
- ## [ Exercise 5.3] ( Chapter05/ 5.03.cpp)
6
+ ## [ Exercise 5.3] ( 5.03.cpp )
7
7
Use the comma operator (§ 4.10, p. 157) to rewrite the while loop from
8
8
§ 1.4.1 (p. 11) so that it no longer requires a block. Explain whether this rewrite im-
9
9
proves or diminishes the readability of this code.
10
- ## [ Exercise 5.4] ( Chapter05/ 5.04.txt)
10
+ ## [ Exercise 5.4] ( 5.04.txt )
11
11
Explain each of the following examples, and correct any problems you detect.
12
12
```
13
13
(a) while (string::iterator iter != s.end()) { /* . . . */ }
14
14
(b) while (bool status = find(word)) { /* . . . */ }
15
15
if (!status) { /* . . . */ }
16
16
```
17
- ## [ Exercise 5.5] ( Chapter05/ 5.05.cpp)
17
+ ## [ Exercise 5.5] ( 5.05.cpp )
18
18
Using an if–else statement, write your own version of the program to
19
19
generate the letter grade from a numeric grade.
20
- ## [ Exercise 5.6] ( Chapter05/ 5.06.cpp)
20
+ ## [ Exercise 5.6] ( 5.06.cpp )
21
21
Rewrite your grading program to use the conditional operator (§ 4.7,
22
22
p. 151) in place of the if–else statement.
23
- ## [ Exercise 5.7] ( Chapter05/ 5.07.txt)
23
+ ## [ Exercise 5.7] ( 5.07.txt )
24
24
Correct the errors in each of the following code fragments:
25
25
```
26
26
(a) if (ival1 != ival2)
@@ -36,26 +36,26 @@ Correct the errors in each of the following code fragments:
36
36
(d) if (ival = 0)
37
37
ival = get_value();
38
38
```
39
- ## [ Exercise 5.8] ( Chapter05/ 5.08.txt)
39
+ ## [ Exercise 5.8] ( 5.08.txt )
40
40
What is a “dangling else”? How are else clauses resolved in C++?
41
- ## [ Exercise 5.9] ( Chapter05/ 5.09.cpp)
41
+ ## [ Exercise 5.9] ( 5.09.cpp )
42
42
Write a program using a series of if statements to count the number of
43
43
vowels in text read from cin.
44
- ## [ Exercise 5.10] ( Chapter05/ 5.10.cpp)
44
+ ## [ Exercise 5.10] ( 5.10.cpp )
45
45
There is one problem with our vowel-counting program as we’ve im-
46
46
plemented it: It doesn’t count capital letters as vowels. Write a program that counts
47
47
both lower- and uppercase letters as the appropriate vowel—that is, your program
48
48
should count both ’a’ and ’A’ as part of aCnt, and so forth.
49
- ## [ Exercise 5.11] ( Chapter05/ 5.11.cpp)
49
+ ## [ Exercise 5.11] ( 5.11.cpp )
50
50
Modify our vowel-counting program so that it also counts the number
51
51
of blank spaces, tabs, and newlines read.
52
- ## [ Exercise 5.12] ( Chapter05/ 5.12.cpp)
52
+ ## [ Exercise 5.12] ( 5.12.cpp )
53
53
Modify our vowel-counting program so that it counts the number of
54
54
occurrences of the following two-character sequences: ff, fl, and fi.
55
- ## [ Exercise 5.13] ( Chapter05/ 5.13.txt)
55
+ ## [ Exercise 5.13] ( 5.13.txt )
56
56
Each of the programs in the highlighted text on page 184 contains a
57
57
common programming error. Identify and correct each error.
58
- ## [ Exercise 5.14] ( Chapter05/ 5.14.cpp)
58
+ ## [ Exercise 5.14] ( 5.14.cpp )
59
59
Write a program to read strings from standard input looking for du-
60
60
plicated words. The program should find places in the input where one word is fol-
61
61
lowed immediately by itself. Keep track of the largest number of times a single repeti-
@@ -65,7 +65,7 @@ else print a message saying that no word was repeated. For example, if the input
65
65
how now now now brown cow cow
66
66
```
67
67
the output should indicate that the word now occurred three times.
68
- ## [ Exercise 5.15] ( Chapter05/ 5.15.txt)
68
+ ## [ Exercise 5.15] ( 5.15.txt )
69
69
Explain each of the following loops. Correct any problems you detect.
70
70
```
71
71
(a) for (int ix = 0; ix != sz; ++ix) { /* . . . */ }
@@ -75,18 +75,18 @@ Explain each of the following loops. Correct any problems you detect.
75
75
for (ix != sz; ++ix) { /* . . . */ }
76
76
(c) for (int ix = 0; ix != sz; ++ix, ++ sz) { /* . . . */ }
77
77
```
78
- ## [ Exercise 5.16] ( Chapter05/ 5.16)
78
+ ## [ Exercise 5.16] ( 5.16 )
79
79
The while loop is particularly good at executing while some condition
80
80
holds; for example, when we need to read values until end-of-file. The for loop is
81
81
generally thought of as a step loop: An index steps through a range of values in a
82
82
collection. Write an idiomatic use of each loop and then rewrite each using the other
83
83
loop construct. If you could use only one loop, which would you choose? Why?
84
- ## [ Exercise 5.17] ( Chapter05/ 5.17.cpp)
84
+ ## [ Exercise 5.17] ( 5.17.cpp )
85
85
Given two vectors of ints, write a program to determine whether
86
86
one vector is a prefix of the other. For vectors of unequal length, compare the num-
87
87
ber of elements of the smaller vector. For example, given the vectors containing 0,
88
88
1, 1, and 2 and 0, 1, 1, 2, 3, 5, 8, respectively your program should return true.
89
- ## [ Exercise 5.18] ( Chapter05/ 5.18.txt)
89
+ ## [ Exercise 5.18] ( 5.18.txt )
90
90
Explain each of the following loops. Correct any problems you detect.
91
91
```
92
92
(a) do
@@ -102,29 +102,29 @@ Explain each of the following loops. Correct any problems you detect.
102
102
int ival = get_response();
103
103
} while (ival);
104
104
```
105
- ## [ Exercise 5.19] ( Chapter05/ 5.19.cpp)
105
+ ## [ Exercise 5.19] ( 5.19.cpp )
106
106
Write a program that uses a do while loop to repetitively request two
107
107
strings from the user and report which string is less than the other.
108
- ## [ Exercise 5.20] ( Chapter05/ 5.20.cpp)
108
+ ## [ Exercise 5.20] ( 5.20.cpp )
109
109
Write a program to read a sequence of strings from the standard input
110
110
until either the same word occurs twice in succession or all the words have been read.
111
111
Use a while loop to read the text one word at a time. Use the break statement to
112
112
terminate the loop if a word occurs twice in succession. Print the word if it occurs
113
113
twice in succession, or else print a message saying that no word was repeated.
114
- ## [ Exercise 5.21] ( Chapter05/ 5.21)
114
+ ## [ Exercise 5.21] ( 5.21 )
115
115
Revise the program from the exercise in § 5.5.1 (p. 191) so that it looks
116
116
only for duplicated words that start with an uppercase letter.
117
- ## [ Exercise 5.22] ( Chapter05/ 5.22.cpp)
117
+ ## [ Exercise 5.22] ( 5.22.cpp )
118
118
The last example in this section that jumped back to begin could be
119
119
better written using a loop. Rewrite the code to eliminate the goto.
120
- ## [ Exercise 5.23] ( Chapter05/ 5.23.cpp)
120
+ ## [ Exercise 5.23] ( 5.23.cpp )
121
121
Write a program that reads two integers from the standard input and
122
122
prints the result of dividing the first number by the second.
123
- ## [ Exercise 5.24] ( Chapter05/ 5.24.cpp)
123
+ ## [ Exercise 5.24] ( 5.24.cpp )
124
124
Revise your program to throw an exception if the second number is
125
125
zero. Test your program with a zero input to see what happens on your system if you
126
126
don’t catch an exception.
127
- ## [ Exercise 5.25] ( Chapter05/ 5.25.cpp)
127
+ ## [ Exercise 5.25] ( 5.25.cpp )
128
128
Revise your program from the previous exercise to use a try block to
129
129
catch the exception. The catch clause should print a message to the user and ask
130
130
them to supply a new number and repeat the code inside the try.
0 commit comments