Chapter 4

advertisement
Self-Review Exercises
4.1
Answer each of the following questions.
a.
b.
c.
d.
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:
a.
b.
c.
d.
4.4
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.
Write C++ statements to accomplish each of the following tasks.
a.
b.
c.
d.
e.
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.
a.
b.
product *= x++;
quotient /= ++x;
4.7
Write single C++ statements that do the following:
a.
b.
c.
d.
e.
f.
g.
h.
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:
a.
{
while ( c <= 5 )
product *= c;
c++;
b.
cin << value;
c. 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;
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++;
a. z = x++ + y;
b. if ( count > 10 )
cout << "Count is greater than 10" << endl;
4.3
c. total -= --x;
d. q %= divisor;
q = q % divisor;
a.
int sum;
int x;
4.4
b. x = 1;
c. sum = 0;
d.
sum += x;
or
sum = sum + x;
e.
4.5
cout << "The sum is: " << sum << end1;
See the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Exercise 4.5 Solution: ex04_05.cpp
// Calculate the sum of the integers from 1 to 10.
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int sum; // stores sum of integers 1 to 10
int x; // counter
x = 1; // count from 1
sum = 0; // initialize sum
while ( x <= 10 ) // loop 10 times
{
sum += x; // add x to sum
x++; // increment x
19
20
21
22
23
} // end while
cout << "The sum is: " << sum << endl;
return 0; // indicate successful termination
} // end main
The sum is: 55
4.6
a.
b.
product = 25, x = 6;
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
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
a.
b.
c.
d.
e.
cin >> x;
cin >> y;
i = 1;
power = 1;
power *= x;
or
power = power * x;
f.
g.
h.
4.8
i++;
if (i <= y )
cout << power << endl;
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
Enter base as an integer: 2
Enter exponent as an integer: 3
8
4.9
a.
Error: Missing the closing right brace of the while body.
Correction: Add closing right brace after the statement c++;.
b.
Error: Used stream insertion instead of stream extraction.
Correction: Change << to >> .
c.
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.
Download