Professional Documents
Culture Documents
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)
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
int main()
{
int num;
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;
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)
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
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>
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;
write a while loop and a do. . .while loop that have the same output.
300 | Chapter 5: Control Structures II (Repetition)
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>
int main()
{
int x, y, z;
x = 4; y = 5;
z = y + 6;
do
{
cout << z << " ";
z = z + 7;
}
while (((z - x) % 4) != 0);
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;
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.