< previous page page_316 next page >

Page 316
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
n = 2;
while (n != 15)
{
    n = n + 2;
    cout << n <<  ;
}
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
a. What is the output of the code as written?
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
b. Correct the code so that it works as intended.
7. The following code segment is supposed to copy one line from the standard input device to the standard output device.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
cin.get(inChar);
while (inChar != \n)
{
    cin.get(inChar);
    cout << inChar;
}
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
a. What is the output if the input line consists of the characters ABCDE?
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
b. Rewrite the code so that it works properly.
8. Does the following program segment need any priming reads? If not, explain why. If so, add the input statement(s) in the proper place. (letter is of type char.)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
while (cin)
{
    while (letter != \n)
    {
        cout << letter;
        cin.get(letter);
    }
    cout << endl;
    cout << Another line read << endl;
    cin.get(letter);
}
9. Write the loop invariant for the following loop. (sum and count are of type int.)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
sum = 0;
count = 0;
while (count < 22)
{
    sum = sum + count;
    count++;
}
10. What sentinel value would you choose for a program that reads telephone numbers as integers?
11. Consider this program:

 
< previous page page_316 next page >