4.1 Answer each of the following questions.
All programs can be written in terms of three types of control statements: , 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 iteration.
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.
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 variable sum
to be of type unsigned int
and initialize it to 0
.
Declare variable x
to be of type unsigned int
and initialize it to 1
.
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 of these unsigned int
variables after the calculation is performed. Assume that, when each statement begins executing, all variables have the integer value 5.
product *= x++;
quotient /= ++x;
4.7 Write single C++ statements or portions of statements that do the following:
Input unsigned int
variable x
with cin
and >>
.
Input unsigned int
variable y
with cin
and >>
.
Declare unsigned int
variable i
and initialize it to 1
.
Declare unsigned int
variable power
and initialize it to 1
.
Multiply variable power
by x
and assign the result to power
.
Preincrement 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
iteration 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’s wrong with the following while
iteration statement?
while (z >= 0) {
sum += z;
}
Sequence, selection and iteration.
if
…else
.
Counter-controlled or definite.
Sentinel, signal, flag or dummy.
x = x + 1;
x += 1;
++x;
x++;
z = x++ + y;
if (count > 10) {
cout << "Count is greater than 10" << endl;
}
total -= --x;
q %= divisor;
q = q % divisor;
unsigned int sum{0};
unsigned int x{1};
sum += x;
or
sum = sum + x;
cout << "The sum is: " << sum << endl;
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
product = 25, x = 6;
quotient = 0, x = 6;
cin >> x;
cin >> y;
unsigned int i{1};
unsigned int power{1};
power *= x;
or
power = power * x;
++i;
if (i <= y)
cout << power << endl;
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
Error: Missing the closing right brace of the while
body.
Correction: Add closing right brace after the statement ++c;
.
Error: Used stream insertion instead of stream extraction.
Correction: Change <<
to >>
.
Error: Semicolon after else
is a logic error. The second output statement always executes.
Correction: Remove the semicolon after else
.
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.