Self-Review Exercises

  1. 4.1 Answer each of the following questions.

    1. All programs can be written in terms of three types of control statements:             , and             .

    2. The              selection statement is used to execute one action when a condition is true or a different action when that condition is false.

    3. Repeating a set of instructions a specific number of times is called              iteration.

    4. When it isn’t known in advance how many times a set of statements will be repeated, a(n)              value can be used to terminate the iteration.

  2. 4.2 Write four different C++ statements that each add 1 to integer variable x.

  3. 4.3 Write C++ statements to accomplish each of the following:

    1. In one statement, assign the sum of the current value of x and y to z and postincrement the value of x.

    2. Determine whether the value of the variable count is greater than 10. If it is, print "Count is greater than 10".

    3. Predecrement the variable x by 1, then subtract it from the variable total.

    4. Calculate the remainder after q is divided by divisor and assign the result to q. Write this statement two different ways.

  4. 4.4 Write C++ statements to accomplish each of the following tasks.

    1. Declare variable sum to be of type unsigned int and initialize it to 0.

    2. Declare variable x to be of type unsigned int and initialize it to 1.

    3. Add variable x to variable sum and assign the result to variable sum.

    4. Print "The sum is: " followed by the value of variable sum.

  5. 4.5 Combine the statements that you wrote in Exercise 4.4 into a program that calculates and prints the sum of the integers from 1 to 10. Use the while statement to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11.

  6. 4.6 State the values of each of these unsigned int variables after the calculation is performed. Assume that, when each statement begins executing, all variables have the integer value 5.

    1. product *= x++;

    2. quotient /= ++x;

  7. 4.7 Write single C++ statements or portions of statements that do the following:

    1. Input unsigned int variable x with cin and >>.

    2. Input unsigned int variable y with cin and >>.

    3. Declare unsigned int variable i and initialize it to 1.

    4. Declare unsigned int variable power and initialize it to 1.

    5. Multiply variable power by x and assign the result to power.

    6. Preincrement variable i by 1.

    7. Determine whether i is less than or equal to y.

    8. Output integer variable power with cout and <<.

  8. 4.8 Write a C++ program that uses the statements in Exercise 4.7 to calculate x raised to the y power. The program should have a while iteration statement.

  9. 4.9 Identify and correct the errors in each of the following:

    1.  

      
      while (c <= 5) {
         product *= c;
         ++c;
      
    2. cin << value;

    3.  

      
      if (gender == 1) {
         cout << "Woman" << endl;
      else; {
         cout << "Man" << endl;
      }
      
  10. 4.10 What’s wrong with the following while iteration statement?

    
    while (z >= 0) {
       sum += z;
    }
    

Answers to Self-Review Exercises

  1. 4.1

    1. Sequence, selection and iteration.

    2. ifelse.

    3. Counter-controlled or definite.

    4. Sentinel, signal, flag or dummy.

  2. 4.2

    
    x = x + 1;
    x += 1;
    ++x;
    x++;
    
  3. 4.3

    1. z = x++ + y;

    2.  

      
      if (count > 10) {
         cout << "Count is greater than 10" << endl;
      }
      
    3. total -= --x;

    4.  

      
      q %= divisor;
      q = q % divisor;
      
  4. 4.4

    1. unsigned int sum{0};

    2. unsigned int x{1};

    3. 
      sum += x;
      or
      sum = sum + x;
      
    4. cout << "The sum is: " << sum << endl;

  5. 4.5 See the following code:

    
     1   // Exercise 4.5: Calculate.cpp
     2   // Calculate the sum of the integers from 1 to 10
     3   #include <iostream>
     4   using namespace std;
     5
     6   int main() {
     7      unsigned int sum{0};
     8      unsigned int x{1};
     9
    10      while (x <= 10) { // while x is less than or equal to 10
    11         sum += x; // add x to sum
    12         ++x; // increment x
    13      }
    14
    15     cout << "The sum is: " << sum << endl;
    16   }
    
    
    The sum is: 55
    
  6. 4.6

    1. product = 25, x = 6;

    2. quotient = 0, x = 6;

  7. 4.7

    1. cin >> x;

    2. cin >> y;

    3. unsigned int i{1};

    4. unsigned int power{1};

    5.  

      
      power *= x;
      or
      power = power * x;
      
    6. ++i;

    7. if (i <= y)

    8. cout << power << endl;

  8. 4.8 See the following code:

    
     1   // Exercise 4.8 Solution: power.cpp
     2   // Raise x to the y power.
     3   #include <iostream>
     4   using namespace std;
     5
     6   int main() {
     7      unsigned int i{1}; // initialize i to begin counting from 1
     8      unsigned int power{1}; // initialize power
     9
    10      cout << "Enter base as an integer: "; // prompt for base
    11      unsigned int x; // base
    12      cin >> x; // input base
    13
    14      cout << "Enter exponent as an integer: "; // prompt for exponent
    15      unsigned int y; // exponent
    16      cin >> y; // input exponent
    17
    18      // count from 1 to y and multiply power by x each time
    19      while (i <= y) {
    20         power *= x;
    21         ++i;
    22      } // end while
    23
    24      cout << power << endl; // display result
    25   } // end main
    
    
    Enter base as an integer: 2
    Enter exponent as an integer: 3
    8
    
  9. 4.9

    1. Error: Missing the closing right brace of the while body.

      Correction: Add closing right brace after the statement ++c;.

    2. Error: Used stream insertion instead of stream extraction.

      Correction: Change << to >>.

    3. Error: Semicolon after else is a logic error. The second output statement always executes.

      Correction: Remove the semicolon after else.

  10. 4.10 The value of the variable z is never changed in the while statement. Therefore, if the loop-continuation condition (z >= 0) is initially true, an infinite loop is created. To prevent the infinite loop, z must be decremented so that it eventually becomes less than 0.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset