< previous page page_498 next page >

Page 498
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
    else
    {
        cin >> int2;
        if ( !cin || int2 > int1)
           cout << Invalid second integer.;
        else
        {
           cin >> int3;
           if ( !cin || int3 == 0)
               cout << Invalid third integer.;
           else
           {
               sum = sum + (int1 + int2) / int3;
               count++;
           }
        }
    }
} while (cin && int1 > 0 && int2 <= int1 && int3 != 0 &&
         count <= 100);
12. Write a While loop (and any pre-loop initializations) corresponding to the following loop invariant and loop postcondition.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
// Invariant (prior to test):
//     prod == 1 * 2 *  * (count-1)
//  && 1 <= count <= 12

// Postcondition:
//     prod == 1 * 2 *  * 11
//  && count == 12
13. The following loop is supposed to add the integers 1 through 100, but the loop is wrong (it's off by one). Prove that it is wrong by determining the loop postcondition. (Write the loop invariant for the code as it stands, then use the AND of the loop invariant and the termination condition to find the postcondition.)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
sum = 0;
i = 0;
while (i <= 100)
{
    i++;
    sum = sum + i;
}
Programming Problems
1. Develop a top-down design and write a C++ program that inputs a two-letter abbreviation for one of the 50 states and prints out the full name of the state. If the abbreviation isn't valid, the program should print an error message and ask for an abbreviation again. The names of the 50 states and their abbreviations are:

 
< previous page page_498 next page >