www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 173 (continued)]

Self-Review Exercises

4.1

Answer each of the following questions.

  1. All programs can be written in terms of three types of control structures:_________, __________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_________repetition.

  4. When it is not known in advance how many times a set of statements will be repeated, a(n)_________value can be used to terminate the repetition.

4.2

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

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

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

  1. Declare variables sum and x to be of type int.

  2. Set variable x to 1.

  3. Set variable sum to 0.

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

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

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.

4.6

State the values of each variable 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;


[Page 174]
4.7

Write single C++ statements that do the following:

  1. Input integer variable x with cin and >>.

  2. Input integer variable y with cin and >>.

  3. Set integer variable i to 1.

  4. Set integer variable power to 1.

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

  6. Postincrement variable i by 1.

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

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

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 repetition statement.

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;
    

4.10

What is wrong with the following while repetition statement?

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


Previous Page
Next Page