www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 230 (continued)]

Self-Review Exercises

5.1

State whether the following are true or false. If the answer is false, explain why.

  1. The default case is required in the switch selection statement.

  2. The break statement is required in the default case of a switch selection statement to exit the switch properly.

  3. The expression ( x > y && a < b ) is true if either the expression x > y is true or the expression a < b is true.

  4. An expression containing the || operator is true if either or both of its operands are TRue.

5.2

Write a C++ statement or a set of C++ statements to accomplish each of the following:

  1. Sum the odd integers between 1 and 99 using a for statement. Assume the integer variables sum and count have been declared.

  2. Print the value 333.546372 in a field width of 15 characters with precisions of 1, 2 and 3. Print each number on the same line. Left-justify each number in its field. What three values print?

  3. Calculate the value of 2.5 raised to the power 3 using function pow. Print the result with a precision of 2 in a field width of 10 positions. What prints?

  4. Print the integers from 1 to 20 using a while loop and the counter variable x. Assume that the variable x has been declared, but not initialized. Print only 5 integers per line. [Hint: Use the calculation x % 5. When the value of this is 0, print a newline character; otherwise, print a tab character.]

  5. Repeat Exercise 5.2 (d) using a for statement.


[Page 231]
5.3

Find the error(s) in each of the following code segments and explain how to correct it (them).

  1. x = 1;
    while ( x <= 10 );
       x++;
    }
    
  2. for ( y = .1; y != 1.0; y += .1 )
       cout << y << endl;
    
  3. switch ( n )
    {
       case 1:
          cout << "The number is 1" << endl;
       case 2:
          cout << "The number is 2" << endl;
          break;
       default:
          cout << "The number is not 1 or 2" << endl;
          break;
    }
    
  4. The following code should print the values 1 to 10.

    n = 1;
    while ( n < 10 )
       cout << n++ << endl;
    


Previous Page
Next Page