You are on page 1of 13

290 | Chapter 5: Control Structures II (Repetition)

8. In a counter-controlled while loop, you must initialize the counter before


the loop, and the body of the loop must contain a statement that changes
the value of the counter variable.
9. A sentinel is a special value that marks the end of the input data. The
sentinel must be similar to, yet differ from, all the data items.
10. A sentinel-controlled while loop uses a sentinel to control the while
loop. The while loop continues to execute until the sentinel is read.
11. An EOF-controlled while loop continues to execute until the program
detects the end-of-file marker.
12. In the Windows console environment, the end-of-file marker is entered using
Ctrl+z (hold the Ctrl key and press z). In the UNIX environment, the
end-of-file marker is entered using Ctrl+d (hold the Ctrl key and press d).
13. A for loop simplifies the writing of a count-controlled while loop.
14. In C++, for is a reserved word.
15. The syntax of the for loop is:
for (initialize statement; loop condition; update statement)
statement

statement is called the body of the for loop.


16. Putting a semicolon at the end of the for loop (before the body of the for
loop) is a semantic error. In this case, the action of the for loop is empty.
17. The syntax of the do. . .while statement is:
do
statement
while (expression);
statement is called the body of the do. . .while loop.
18. Both while and for loops are called pretest loops. A do. . .while loop is
called a posttest loop.
19. The while and for loops may not execute at all, but the do. . .while loop
always executes at least once.
20. Executing a break statement in the body of a loop immediately terminates
the loop.
21. Executing a continue statement in the body of a loop skips the loop’s
remaining statements and proceeds with the next iteration.
22. When a continue statement executes in a while or do. . .while loop,
the expression update statement in the body of the loop may not execute.
23. After a continue statement executes in a for loop, the update statement
is the next statement executed.
Exercises | 291

EXERCISES
1. Mark the following statements as true or false.
a. In a counter-controlled while loop, it is not necessary to initialize the
loop control variable.
b. It is possible that the body of a while loop may not execute at all.
c. In an infinite while loop, the while expression (the decision maker) is
initially false, but after the first iteration it is always true.
d. The while loop:
j = 0;
while (j <= 10)
j++;
5
terminates if j > 10.
e. A sentinel-controlled while loop is an event-controlled while loop
whose termination depends on a special value.
f. A loop is a control structure that causes certain statements to execute
over and over.
g. To read data from a file of an unspecified length, an EOF-controlled
loop is a good choice.
h. When a while loop terminates, the control first goes back to the
statement just before the while statement, and then the control goes
to the statement immediately following the while loop.
2. What is the output of the following C++ code?
count = 1;
y = 100;
while (count < 100)
{
y = y - 1;
count++;
}
cout << " y = " << y << " and count = " << count << endl;
3. What is the output of the following C++ code?
num = 5;
while (num > 5)
num = num + 2;
cout << num << endl;
4. What is the output of the following C++ code?
num = 1;
while (num < 10)
{
292 | Chapter 5: Control Structures II (Repetition)

cout << num << " ";


num = num + 2;
}
cout << endl;
5. When does the following while loop terminate?
ch = 'D';
while ('A' <= ch && ch <= 'Z')
ch = static_cast<char>(static_cast<int>(ch) + 1);
6. Suppose that the input is 38 45 71 4 -1. What is the output of the
following code? Assume all variables are properly declared.
cin >> sum;
cin >> num;

for (j = 1; j <= 3; j++)


{
cin >> num;
sum = sum + num;
}
cout << "Sum = " << sum << endl;
7. Suppose that the input is 38 45 71 4 -1. What is the output of the
following code? Assume all variables are properly declared.
cin >> sum;
cin >> num;

while (num != -1)


{
sum = sum + num;
cin >> num;
}
cout << "Sum = " << sum << endl;
8. Suppose that the input is 38 45 71 4 -1. What is the output of the
following code? Assume all variables are properly declared.
cin >> num;
sum = num;

while (num != -1)


{
cin >> num;
sum = sum + num;
}
cout << "Sum = " << sum << endl;
9. Suppose that the input is 38 45 71 4 -1. What is the output of the
following code? Assume all variables are properly declared.
sum = 0;
cin >> num;
Exercises | 293

while (num != -1)


{
sum = sum + num;
cin >> num;
}
cout << "Sum = " << sum << endl;
10. Correct the following code so that it finds the sum of 10 numbers.
sum = 0;

while (count < 10)


cin >> num;
sum = sum + num;
count++;
11. What is the output of the following program? 5
#include <iostream>

using namespace std;

int main()
{
int x, y, z;

x = 4; y = 5;
z = y + 6;

while(((z - x) % 4) != 0)
{
cout << z << " ";
z = z + 7;
}
cout << endl;

return 0;
}
12. Suppose that the input is:
58 23 46 75 98 150 12 176 145 -999

What is the output of the following program?


#include <iostream>

using namespace std;

int main()
{
int num;

cin >> num;


294 | Chapter 5: Control Structures II (Repetition)

while (num != -999)


{
cout << num % 25 << " ";
cin >> num;
}

cout << endl;

return 0;
}
13. Given:
for (i = 12; i <= 25; i++)
cout << i;
a. The seventh integer printed is _______________________.
b. The statement produces ______________________ lines of output.
c. If i++ were changed to i--, a compilation error would result. True or
false?
14. Given that the following code is correctly inserted into a program, state its
entire output as to content and form.
num = 0;
for (i = 1; i <= 4; i++)
{
num = num + 10 * (i - 1);
cout << num << " ";
}
cout << endl;
15. Given that the following code is correctly inserted into a program, state its
entire output as to content and form.
j = 2;
for (i = 0; i <= 5; i++)
{
cout << j << " ";
j = 2 * j + 3;
}
cout << j << " " << endl;
16. Assume that the following code is correctly inserted into a program:
int s = 0;

for (i = 0; i < 5; i++)


{
s = 2 * s + i;
cout << s << " ";
}
cout << endl;
Exercises | 295

a. What is the final value of s?


(i) 11 (ii) 4 (iii) 26 (iv) none of these
b. If a semicolon is inserted after the right parentheses in the for loop
statement, what is the final value of s?
(i) 0 (ii) 1 (iii) 2 (iv) 5 (v) none of these
c. If the 5 is replaced with a 0 in the for loop control expression, what is
the final value of s?
(i) 0 (ii) 1 (iii) 2 (iv) none of these
17. State what output, if any, results from each of the following statements:
a.
for (i = 1; i <= 1; i++)
cout << "*"; 5
cout << endl;

b.
for (i = 2; i >= 1; i++)
cout << "*";
cout << endl;

c.
for (i = 1; i <= 1; i--)
cout << "*";
cout << endl;

d.
for (i = 12; i >= 9; i--)
cout << "*";
cout << endl;

e.
for (i = 0; i <= 5; i++)
cout << "*";
cout << endl;

f.
for (i = 1; i <= 5; i++)
{
cout << "*";
i = i + 1;
}
cout << endl;
18. Write a for statement to add all the multiples of 3 between 1 and 100.
296 | Chapter 5: Control Structures II (Repetition)

19. What is the exact output of the following program?


#include <iostream>

using namespace std;

int main()
{
int counter;
for (counter = 7; counter <= 16; counter++)
switch (counter % 10)
{
case 0:
cout << ", ";
break;
case 1:
cout << "OFTEN ";
break;
case 2:
case 8:
cout << "IS ";
break;
case 3:
cout << "NOT ";
break;
case 4:
case 9:
cout << "DONE ";
break;
case 5:
cout << "WELL";
break;
case 6:
cout << ".";
break;
case 7:
cout << "WHAT ";
break;
default:
cout << "Bad number. ";
}
cout << endl;

return 0;
}
20. Suppose that the input is 5 3 8. What is the output of the following code?
Assume all variables are properly declared.
Exercises | 297

cin >> a >> b >> c;


for (j = 1; j < a; j++)
{
d = b + c;
b = c;
c = d;
cout << c << " ";
}
cout << endl;
21. What is the output of the following C++ program segment? Assume all
variables are properly declared.
for (j = 0; j < 8; j++)
{
cout << j * 25 << " - ";
5
if (j != 7)
cout << (j + 1) * 25 - 1 << endl;
else
cout << (j + 1) * 25 << endl;
}
22. The following program has more than five mistakes that prevent it from
compiling and/or running. Correct all such mistakes:
#include <iostream>

using namespace std;


const int N = 2,137;

main ()
{
int a, b, c, d:

a := 3;
b = 5;
c = c + d;
N = a + n;
for (i = 3; i <= N; i++)
{
cout << setw(5) << i;
i = i + 1;
}
return 0;
}
23. Which of the following apply to the while loop only? To the do. . .while
loop only? To both?
a. It is considered a conditional loop.
b. The body of the loop executes at least once.
298 | Chapter 5: Control Structures II (Repetition)

c. The logical expression controlling the loop is evaluated before the loop
is entered.
d. The body of the loop may not execute at all.
24. How many times will each of the following loops execute? What is the
output in each case?
a.
x = 5; y = 50;
do
x = x + 10;
while (x < y);
cout << x << " " << y << endl;
b.
x = 5; y = 80;
do
x = x * 2;
while (x < y);
cout << x << " " << y << endl;

c.
x = 5; y = 20;
do
x = x + 2;
while (x >= y);
cout << x << " " << y << endl;
d.
x = 5; y = 35;
while (x < y)
x = x + 10;
cout << x << " " << y << endl;
e.
x = 5; y = 30;
while (x <= y)
x = x * 2;
cout << x << " " << y << endl;
f.
x = 5; y = 30;
while (x > y)
x = x + 2;
cout << x << " " << y << endl;
25. The do. . .while loop in the following program is supposed to read some
numbers until it reaches a sentinel (in this case, -1). It is supposed to add all
of the numbers except for the sentinel. If the data looks like:
12 5 30 48 -1
Exercises | 299

the program does not add the numbers correctly. Correct the program so that it adds
the numbers correctly.
#include <iostream>

using namespace std;


int main()
{
int total = 0,
count = 0,
number;
do
{
cin >> number;
total = total + number;
count++; 5
}
while (number != -1);

cout << "The number of data read is " << count << endl;
cout << "The sum of the numbers entered is " << total
<< endl;

return 0;
}
26. Using the same data as in Exercise 25, the following two loops also fail.
Correct them.
a.
cin >> number;

while (number != -1)


total = total + number;
cin >> number;
cout << endl;
cout << total << endl;
b.
cin >> number;
while (number != -1)
{
cin >> number;
total = total + number;
}
cout << endl;
cout << total << endl;
27. Given the following program segment:
for (number = 1; number <= 10; number++)
cout << setw(3) << number;

write a while loop and a do. . .while loop that have the same output.
300 | Chapter 5: Control Structures II (Repetition)

28. Given the following program segment:


j = 2;
for (i = 1; i <= 5; i++);
{
cout << setw(4) << j;
j = j + 5;
}
cout << endl;

write a while loop and a do. . .while loop that have the same output.
29. What is the output of the following program?
#include <iostream>

using namespace std;

int main()
{
int x, y, z;
x = 4; y = 5;
z = y + 6;
do
{
cout << z << " ";
z = z + 7;
}
while (((z - x) % 4) != 0);

cout << endl;

return 0;
}
30. To learn how nested for loops work, do a walk-through of the
following program segments and determine, in each case, the exact
output.
a.
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 5; j++)
cout << setw(3) << i * j;
cout << endl;
}
Exercises | 301

b.
int i, j;

for (i = 1; i <= 5; i++)


{
for (j = 1; j <= 5; j++)
cout << setw(3) << i;
cout << endl;
}
c.
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = (i + 1); j <= 5; j++)
cout << setw(5) << j; 5
cout << endl;
}
d.
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
cout << setw(3) << j;
cout << endl;
}
e.
const int M = 10;
const int N = 10;
int i, j;

for (i = 1; i <= M; i++)


{
for (j = 1; j <= N; j++)
cout << setw(3) << M * (i - 1) + j;
cout << endl;
}
f.
int i, j;

for (i = 1; i <= 9; i++)


{
for (j = 1; j <= (9 - i); j++)
cout << " ";
for (j = 1; j <= i; j++)
cout << setw(1) << j;
for (j = (i - 1); j >= 1; j--)
cout << setw(1) << j;
cout << endl;
}
302 | Chapter 5: Control Structures II (Repetition)

PROGRAMMING EXERCISES

1. Write a program that prompts the user to input an integer and then outputs
both the individual digits of the number and the sum of the digits. For
example, it should output the individual digits of 3456 as 3 4 5 6, output
the individual digits of 8030 as 8 0 3 0, output the individual digits of
2345526 as 2 3 4 5 5 2 6, output the individual digits of 4000 as 4 0 0
0, and output the individual digits of -2345 as 2 3 4 5.
2. Write a program that prompts the user to input an integer and then outputs the
number with the digits reversed. For example, if the input is 12345, the output
should be 54321. Your program must also output 5000 as 0005 and 980 as 089.
3. Rewrite the program of Example 5-5, Telephone Digits. Replace the state-
ments from Line 10 to Line 28 so that it uses only a switch structure to find
the digit that corresponds to an uppercase letter.
4. The program Telephone Digits outputs only telephone digits that corre-
spond to uppercase letters. Rewrite the program so that it processes both
uppercase and lowercase letters and outputs the corresponding telephone
digit. If the input is other than an uppercase or lowercase letter, the program
must output an appropriate error message.
5. To make telephone numbers easier to remember, some companies use letters
to show their telephone number. For example, using letters, the telephone
number 438-5626 can be shown as GET LOAN. In some cases, to make a
telephone number meaningful, companies might use more than seven letters.
For example, 225-5466 can be displayed as CALL HOME, which uses eight
letters. Write a program that prompts the user to enter a telephone number
expressed in letters and outputs the corresponding telephone number in
digits. If the user enters more than 7 letters, then process only the first seven
letters. Also output the – (hyphen) after the third digit. Allow the user to use
both uppercase and lowercase letters as well as spaces between words. More-
over, your program should process as many telephone numbers as the user
wants.
6. Write a program that reads a set of integers, and then finds and prints the sum
of the even and odd integers.
7. Write a program that prompts the user to input a positive integer. It should
then output a message indicating whether the number is a prime number.
(Note: An even number is prime if it is 2. An odd integer is prime if it is not
divisible by any odd integer less than or equal to the square root of the
number.)
8. Let n = akak-1ak-2. . .a1a0 be an integer and t = a0 - a1 + a2 -    + (-1)k ak. It
is known that n is divisible by 11 if and only if t is divisible by 11. For
example, suppose that n = 8784204. Then t = 4 - 0 + 2 - 4 + 8 - 7 + 8 = 11.
Because 11 is divisible by 11, it follows that 8784204 is divisible by 11.

You might also like