1: |
Consider the following two code fragments for
counting spaces and newlines:
// Version 1
while (cin.get(ch)) // quit on eof
{
if (ch == ' ')
spaces++;
if (ch == '\n')
newlines++;
}
// Version 2
while (cin.get(ch)) // quit on eof
{
if (ch == ' ')
spaces++;
else if (ch == '\n')
newlines++;
}
What advantages, if any, does the second form have
over the first? |
A: |
Both
versions give the same answers, but the if else version is more
efficient. Consider what happens, for example, when ch is a
space. Version 1, after incrementing spaces, tests to see whether the
character is a newline. This wastes time because the program already has
established that ch is a space and hence could not be a newline.
Version 2, in the same situation, skips the newline test. |
2: |
In
Listing 6.2, what is the effect of replacing ++ch with
ch+1? |
A: |
Both
++ch and ch + 1 have the same numerical value. But ++ch
is type char and prints as a character, while ch + 1,
because it adds a char to an int, is type int
and prints as a number. |
3: |
Consider carefully the following program:
#include <iostream>
using namespace std;
int main()
{
char ch;
int ct1, ct2;
ct1 = ct2 = 0;
while ((ch = cin.get()) != '$')
{
cout << ch;
ct1++;
if (ch = '$')
ct2++;
cout << ch;
}
cout <<"ct1 = " << ct1 << ", ct2 = " << ct2 << "\n";
return 0;
}
Suppose we provide the following input, where
represents pressing Enter:
Hi!
Send $10 or $20 now!
What is the output? (Recall that input is buffered.) |
A: |
Because the program uses ch = '$' instead of ch == '$',
the combined input and output looks like this:
Hi!
H$i$!$
$Send $10 or $20 now!
S$e$n$d$ $ct1 = 9, ct2 = 9
Each character is converted to the $
character before being printed the second time. Also, the value of the
expression ch = $ is the code for the $ character,
hence nonzero, hence true; so ct2 is incremented each time. |
4: |
Construct logical expressions to represent the
following conditions:
-
weight is greater than or equal to 115 but
less than 125.
-
ch is q or Q.
-
x is even but is not 26.
-
x is even but is not a multiple of 26.
-
donation is in the range 1000?000 or
guest is 1.
-
ch is a lowercase letter or an uppercase
letter (assume the lowercase letters are coded sequentially and that
the uppercase letters are coded sequentially but that there is a gap
in the code between uppercase and lowercase).
|
A: |
-
weight >= 115 && weight < 125
-
ch == 'q' || ch == 'Q'
-
x % 2 == 0 && x != 26
-
x % 2 == 0 && !(x % 26 == 0)
-
donation >= 1000 && donation <= 2000 || guest
== 1
-
(ch >= 'a' && ch <= 'z') ||(ch >= 'A' && ch <=
'Z')
|
5: |
In English the statement "I will not not speak" means
the same as "I will speak." In C++, is !!x the same as x? |
A: |
Not
necessarily. For example, if x is 10, then !x is 0 and
!!x is 1. However, if x is a bool variable,
then !!x is x. |
6: |
Construct a conditional expression that is equal to
the absolute value of a variable. That is, if a variable x is
positive, the value of the expression is just x, but if x
is negative, the value of the expression is -x, which is
positive. |
A: |
(x < 0)? -x : x
or
(x >= 0)? x : -x;
|
7: |
Rewrite the following fragment using switch:
if (ch == 'A')
a_grade++;
else if (ch == 'B')
b_grade++;
else if (ch == 'C')
c_grade++;
else if (ch == 'D')
d_grade++;
else
f_grade++;
|
A: |
switch (ch)
{
case 'A': a_grade++;
break;
case 'B': b_grade++;
break;
case 'C': c_grade++;
break;
case 'D': d_grade++;
break;
default: f_grade++;
break;
}
|
8: |
In
Listing 6.10, what advantage would there be in using character
labels, such as a and c, instead of numbers for the
menu choices and switch cases? (Hint: Think about what happens if the
user types q in either case and what happens if the user types
5 in either case.) |
A: |
If
you use integer labels and the user types a noninteger such as q, the
program hangs up because integer input can't process a character. But if
you use character labels and the user types an integer such as 5,
character input will process 5 as a character. Then the default part of
the switch can suggest entering another character. |
9: |
Consider the following code fragment:
int line = 0;
char ch;
while (cin.get(ch))
{
if (ch == 'Q')
break;
if (ch != '\n')
continue;
line++;
}
Rewrite this code without using break or
continue. |
A: |
Here
is one version:
int line = 0;
char ch;
while (cin.get(ch) && ch != 'Q')
{
if (ch == '\n')
line++;
}
|