November 2001
Beginner
1128 pages
29h 12m
English
| 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? |
| 2: | In Listing 6.2, what is the effect of replacing ++ch with ch+1? |
| 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 ... |
Read now
Unlock full access