www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 174 (continued)]

Answers to Self-Review Exercises

4.1

a) Sequence, selection and repetition. b) if...else. c) Counter-controlled or definite. d) Sentinel, signal, flag or dummy.

4.2

x = x + 1;

x += 1

++x;

x++;

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
  1. int sum;
    int x;

  2. x = 1;

  3. sum = 0;

  4. sum += x;
    or
    sum = sum + x;

  5. cout << "The sum is: " << sum << end1;

4.5

See the following code:


[Page 175]
1 // Exercise 4.5 Solution: ex04_05.cpp 2 // Calculate the sum of the integers from 1 to 10. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int sum; // stores sum of integers 1 to 10 10 int x; // counter 11 12 x = 1; // count from 1 13 sum = 0; // initialize sum 14 15 while ( x <= 10 ) // loop 10 times 16 { 17 sum += x; // add x to sum 18 x++; // increment x 19 } // end while 20 21 cout << "The sum is: " << sum << endl; 22 return 0; // indicate successful termination 23 } // end main

 The sum is: 55


4.6
  1. product = 25, x = 6;

  2. quotient = 0, x = 6;

 1  // Exercise 4.6 Solution: ex04_06.cpp
 2  // Calculate the value of product and quotient.
 3  #include <iostream>
 4  using std::cout;
 5  using std::endl;
 6
 7  int main()
 8  {
 9     int x = 5;
10     int product = 5;
11     int quotient = 5;
12
13     // part a
14     product *= x++; // part a statement
15     cout << "Value of product after calculation: " << product << endl;
16     cout << "Value of x after calculation: " << x << endl << endl;
17
18     // part b
19     x = 5; // reset value of x
20     quotient /= ++x; // part b statement
21     cout << "Value of quotient after calculation: " << quotient << endl;
22     cout << "Value of x after calculation: " << x << endl << endl;
23     return 0; // indicate successful termination
24  } // end main


[Page 176]

 Value of product after calculation: 25
 Value of x after calculation: 6

 Value of quotient after calculation: 0
 Value of x after calculation: 6


4.7
  1. cin >> x;

  2. cin >> y;

  3. i = 1;

  4. power = 1;

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

  7. if (i <= y )

  8. cout << power << endl;

4.8

See the following code:

 1  // Exercise 4.8 Solution: ex04_08.cpp
 2  // Raise x to the y power.
 3  #include <iostream>
 4  using std::cout;
 5  using std::cin;
 6  using std::endl;
 7
 8  int main()
 9  {
10     int x; // base
11     int y; // exponent
12     int i; // counts from 1 to y
13     int power; // used to calculate x raised to power y
14
15     i = 1; // initialize i to begin counting from 1
16     power = 1; // initialize power
17
18     cout << "Enter base as an integer: "; // prompt for base
19     cin >> x; // input base
20
21     cout << "Enter exponent as an integer: "; // prompt for exponent
22     cin >> y; // input exponent
23
24     // count from 1 to y and multiply power by x each time
25     while ( i <= y )
26     {
27        power *= x;
28        i++;
29     } // end while
30
31     cout << power << endl; // display result
32     return 0; // indicate successful termination
33  } // end main


[Page 177]

 Enter base as an integer: 2
 Enter exponent as an integer: 3
 8


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 results in a logic error. The second output statement will always be executed.

    Correction: Remove the semicolon after else.

4.10

The value of the variable z is never changed in the while statement. Therefore, if the loopcontinuation 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.


Previous Page
Next Page