< previous page page_317 next page >

Page 317
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
#include <iostream.h>

typedef int Boolean;
const Boolean TRUE = 1;
const Boolean FALSE = 0;

const int LIMIT = 8;

int main()
{
    int     sum;
    int     i;
    int     number;
    Boolean finished;

    sum = 0;
    i = 1;
    finished = FALSE;
    while (i <= LIMIT && !finished)
    {
        cin >> number;
        if (number > 0)
            sum = sum + number;
        else if (number == 0)
            finished = TRUE;
        i++;
    }
    cout << End of test.  << sum <<   << number << endl;
    return 0;
}
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
and these data values:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
5 6 -3 7 -4 0 5 8 9
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
a. What are the contents of sum and number after exit from the loop?
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
b. Does the data fully test the program? Explain your answer.
12. Write the invariant for an EOF-controlled loop that reads integer values, counts them, sums them, and sums the squares of the values.
13. Here is a simple count-controlled loop:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
count = 1;
while (count < 20)
    count++;
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
a. List three ways of changing the loop so that it executes 20 times instead of 19.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
b. Which of those changes makes the value of count range from 1 through 21?
14. What is the output of the following program segment? (All variables are of type int.)

 
< previous page page_317 next page >