• Home
  • Textbooks
  • C++ Programming: From Problem Analysis to Program Design
  • Control Structures II (Repetition)

C++ Programming: From Problem Analysis to Program Design

D. S. Malik

Chapter 5

Control Structures II (Repetition) - all with Video Answers

Educators


Chapter Questions

04:53

Problem 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++;
terminates if j > 10.
e. A sentinel-controlled while loop is an event-controlled while loop whose termination depends on a special value.
f. $\quad$ 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.

Prachita Kush
Prachita Kush
Numerade Educator
01:15

Problem 2

. What is the output of the following C++ code?
int count = 1;
int y = 100;
while (count < 100)
{
y = y - 1;
count++;
}
cout << " y = " << y << " and count = " << count << endl;

Willis James
Willis James
Numerade Educator
01:15

Problem 3

What is the output of the following C++ code?
int num = 5;
while (num > 5)
num = num + 2;
cout << num << endl;

Willis James
Willis James
Numerade Educator
01:15

Problem 4

What is the output of the following C++ code?
int num = 1;
while (num < 10)
{
cout << num << " ";
num = num + 2;
}
cout << endl;

Willis James
Willis James
Numerade Educator
01:08

Problem 5

When does the following while loop terminate?
ch = 'D';
while ('A' <= ch && ch <= 'Z')
ch = static_cast<char>(static_cast<int>(ch) + 1);

Mohamed Mohamed
Mohamed Mohamed
Numerade Educator
07:05

Problem 6

Suppose that the input is 38 35 71 14 -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;

Kumareshwaran Rathinasabapathy
Kumareshwaran Rathinasabapathy
Numerade Educator
05:11

Problem 7

Suppose that the input is 38 35 71 14 -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;

Kumareshwaran Rathinasabapathy
Kumareshwaran Rathinasabapathy
Numerade Educator
05:11

Problem 8

Suppose that the input is 38 35 71 14 -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;

Kumareshwaran Rathinasabapathy
Kumareshwaran Rathinasabapathy
Numerade Educator
05:11

Problem 9

Suppose that the input is 38 35 71 14 -1. What is the output of the
following code? Assume all variables are properly declared.
sum = 0;
cin >> num;
while (num != -1)
{
sum = sum + num;
cin >> num;
}
cout << "Sum = " << sum << endl;

Kumareshwaran Rathinasabapathy
Kumareshwaran Rathinasabapathy
Numerade Educator
07:05

Problem 10

Correct the following code so that it finds the sum of 20 numbers.
sum = 0;
while (count < 20)
cin >> num;
sum = sum + num;
count++;

Kumareshwaran Rathinasabapathy
Kumareshwaran Rathinasabapathy
Numerade Educator
01:49

Problem 11

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;
while(((z - x) % 4) != 0)
{
cout << z << " ";
z = z + 7;
}
cout << endl;
return 0;
}

Ernest Castorena
Ernest Castorena
Numerade Educator
05:11

Problem 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;
while (num != -999)
{
cout << num % 25 << " ";
cin >> num;
}
cout << endl;
return 0;
}

Kumareshwaran Rathinasabapathy
Kumareshwaran Rathinasabapathy
Numerade Educator
06:18

Problem 13

The following program is designed to input two numbers and output their sum. It asks the user if he/she would like to run the program. If the answer is $\mathrm{Y}$ or $\mathrm{y},$ it prompts the user to enter two numbers. After adding the numbers and displaying the results, it again asks the user if he/she would like to add more numbers. However, the program fails to do so. Correct the program so that it works properly.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char response;
double num1;
double num2;
cout << "This program adds two numbers." << endl;
cout << "Would you like to run the program: (Y/y) ";
cin >> response;
cout << endl;
cout << fixed << showpoint << setprecision(2);
while (response == 'Y' && response == 'y')
{
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << endl;
cout << num1 << " + " << num2 << " = " << (num1 - num2)
<< endl;
cout << "Would you like to add again: (Y/y) ";
cin >> response;
cout << endl;
}
return 0;
}

Harriet O'Brien
Harriet O'Brien
Numerade Educator
View

Problem 14

What is the output of the following program segment?
int count = 0;
while (count++ < 10)
cout << "This loop can repeat statements." << endl;

Gio Maya
Gio Maya
Numerade Educator
View

Problem 15

What is the output of the following program segment?
int count = 5;
while (--count > 0)
cout << count << " ";
cout << endl;

Gio Maya
Gio Maya
Numerade Educator
View

Problem 16

What is the output of the following program segment?
int count = 5;
while (count-- > 0)
cout << count << " ";
cout << endl;

Gio Maya
Gio Maya
Numerade Educator
View

Problem 17

What is the output of the following program segment?
int count = 1;
while (count++ <= 5)
cout << count * (count - 2) << " ";
cout << endl;

Gio Maya
Gio Maya
Numerade Educator
02:16

Problem 18

What type of loop, such as counter-control and sentinel-control, will you use in each of the following situations?
a. Sum the following series: 1 + (2 / 1) + (3 / 2) + (4 / 3) + (5 / 4)
+ ... + (10 / 9)
b. Sum the following numbers, except the last number: 17, 32, 62, 48, 58, -1
c. A file contains an employee’s salary. Update the employee’s salary.

Darren Wilson
Darren Wilson
Numerade Educator
02:57

Problem 19

Consider the following for loop:
int j, s;
s = 0;
for (j = 1; j <= 10; j++)
s = s + j * (j - 1);
In this for loop, identify the loop control variable, the initialization statement, the loop condition, the update statement, and the statement that updates the value of $\mathbf{s}$

Mayank Tripathi
Mayank Tripathi
Numerade Educator
02:57

Problem 20

Given that the following code is correctly inserted into a program, state its entire output as to content and form. (Assume all variables are properly declared.)
num = 0;
for (i = 1; i <= 4; i++)
{
num = num + 10 * (i - 1);
cout << num << " ";
}
cout << endl;

Mayank Tripathi
Mayank Tripathi
Numerade Educator
03:26

Problem 21

Given that the following code is correctly inserted into a program, state its entire output as to content and form. (Assume all variables are properly declared.)
j = 2;
for (i = 0; i <= 5; i++)
{
cout << j << " ";
j = 2 * j + 3;
}
cout << j << " " << endl;

Ernest Castorena
Ernest Castorena
Numerade Educator
02:55

Problem 22

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;
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 parenthesis 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

Ernest Castorena
Ernest Castorena
Numerade Educator
01:32

Problem 23

State what output, if any, results from each of the following statements:
a. for (i = 1; i <= 1; i++)
cout << "*";
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;

Mohamed Mohamed
Mohamed Mohamed
Numerade Educator
05:41

Problem 24

Write a for statement to add all the multiples of 3 between 1 and 100

Sandip Ranjan
Sandip Ranjan
Numerade Educator
00:59

Problem 25

What is the output of the following code? Is there a relationship between the variables x and y? If yes, state the relationship? What is the output?
int x = 19683;
int i;
int y = 0;
for (i = x; i >= 1; i = i / 3)
y++;
cout << "x = " << x << ", y = " << y << endl;

AG
Ankit Gupta
Numerade Educator
01:49

Problem 26

Suppose that the input is 5 3 8. What is the output of the following code? Assume all variables are properly declared.
cin >> a >> b >> c;
for (j = 1; j < a; j++)
{
d = b + c;
b = c;
c = d;
cout << c << " ";
}
cout << endl;

Ernest Castorena
Ernest Castorena
Numerade Educator
View

Problem 27

What is the output of the following $\mathrm{C}++$ program segment? Assume all variables are properly declared.
for (j = 0; j < 8; j++)
{
cout << j * 25 << " - ";
if (j != 7)
cout << (j + 1) * 25 - 1 << endl;
else
cout << (j + 1) * 25 << endl;
}

Gio Maya
Gio Maya
Numerade Educator
07:05

Problem 28

Suppose that the input is 38 35 71 44 -1. What is the output of the following code? Assume all variables are properly declared.
sum = 0;
cin >> num;
for (j = 1; j <= 3; j++)
{
cin >> num;
sum = sum + num;
}
cout << "Sum = " << sum << endl;

Kumareshwaran Rathinasabapathy
Kumareshwaran Rathinasabapathy
Numerade Educator
02:29

Problem 29

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.
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.

Ernest Castorena
Ernest Castorena
Numerade Educator
01:00

Problem 30

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;
}

Vysakh M
Vysakh M
Numerade Educator
04:35

Problem 31

What is the difference between a pretest loop and a posttest loop?

Willis James
Willis James
Numerade Educator
01:47

Problem 32

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;

Vysakh M
Vysakh M
Numerade Educator
08:23

Problem 33

Write an input statement validation loop that prompts the user to enter a number less than 20 or greater than 75.

Willis James
Willis James
Numerade Educator
02:57

Problem 34

Rewrite the following as a for loop.
int i = 0, value = 0;
while (i <= 20)
{
if (i % 2 == 0 && i <= 10)
value = value + i * i;
else if (i % 2 == 0 && i > 10)
value = value + i;
else
value = value - i;
i = i + 1;
}
cout << "value = " << value << endl;
What is the output of this loop?

Mayank Tripathi
Mayank Tripathi
Numerade Educator
02:57

Problem 35

Write the while loop of Exercise 34 as a do....while loop.

Mayank Tripathi
Mayank Tripathi
Numerade Educator
06:18

Problem 36

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
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++;
}
while (number != -1);
cout << "The number of data read is " << count << endl;
cout << "The sum of the numbers entered is " << total
<< endl;
return 0;
}

Harriet O'Brien
Harriet O'Brien
Numerade Educator
02:00

Problem 37

Using the same data as in Exercise 36, the following loop also fails. Correct it.
cin >> number;
while (number != -1)
total = total + number;
cin >> number;
cout << endl;
cout << total << endl;

Mayank Tripathi
Mayank Tripathi
Numerade Educator
02:00

Problem 38

Using the same data as in Exercise 36, the following loop also fails. Correct it.
cin >> number;
while (number != -1)
{
cin >> number;
total = total + number;
}
cout << endl;
cout << total << endl;

Mayank Tripathi
Mayank Tripathi
Numerade Educator
02:57

Problem 39

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.

Mayank Tripathi
Mayank Tripathi
Numerade Educator
View

Problem 40

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.

Gio Maya
Gio Maya
Numerade Educator
01:49

Problem 41

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;
}

Ernest Castorena
Ernest Castorena
Numerade Educator
02:57

Problem 42

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;
cout << endl;
}
b. int i, j;
for (i = 1; i <= 5; i++)
{
for (j = (i + 1); j <= 5; j++)
cout << setw(5) << j;
cout << endl;
}
c. int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
cout << setw(3) << j;
cout << endl;
}
d. 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;
}
e. 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;
}

Mayank Tripathi
Mayank Tripathi
Numerade Educator
View

Problem 43

What is the output of the following program segment?
int count = 1;
do
cout << count *(count - 2) << " ";
while (count++ <= 5);
cout << endl;

Gio Maya
Gio Maya
Numerade Educator
01:15

Problem 44

What is the output of the following code?
int num = 12;
while (num >= 0)
{
if (num % 5 == 0)
break;
cout << num << " ";
num = num - 2;
}
cout << endl;

Willis James
Willis James
Numerade Educator
01:15

Problem 45

What is the output of the following code?
int num = 12;
while (num >= 0)
{
if (num % 5 == 0)
{
num++;
continue;
}
cout << num << " ";
num = num - 2;
}
cout << endl;

Willis James
Willis James
Numerade Educator
04:35

Problem 46

What does a break statement do in a loop?

Willis James
Willis James
Numerade Educator