www.gibmonks.com

Main Page

[Page 173 (continued)]

Self-Review Exercises

 4.1 Answer each of the following questions. All programs can be written in terms of three types of control structures:_________, __________and_________.The_________selection statement is used to execute one action when a condition is TRue or a different action when that condition is false.Repeating a set of instructions a specific number of times is called_________repetition.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: In one statement, assign the sum of the current value of x and y to z and postincrement the value of x.Determine whether the value of the variable count is greater than 10. If it is, print "Count is greater than 10."Predecrement the variable x by 1, then subtract it from the variable total.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. Declare variables sum and x to be of type int.Set variable x to 1.Set variable sum to 0.Add variable x to variable sum and assign the result to variable sum.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. product *= x++;quotient /= ++x; [Page 174] 4.7 Write single C++ statements that do the following: Input integer variable x with cin and >>.Input integer variable y with cin and >>.Set integer variable i to 1.Set integer variable power to 1.Multiply variable power by x and assign the result to power.Postincrement variable i by 1.Determine whether i is less than or equal to y.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: ```while ( c <= 5 ) { product *= c; c++; ```cin << value;```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; ```