Unlock access to all the studying documents.
View Full Document
Engineering Problem Solving with C++, 3e Chapter 4 Test Bank
2. A C++ while loop tests the controlling condition before the first iteration of the loop.
3. A C++ do while loop tests the controlling condition before the first iteration of the loop.
4. Show the program trace of the following program using the input stream 5 2 -1 10
#include <iostream>
using namespace std;
int main()
{ int A, B, N;
A = 3;
B = 6;
cout << “Enter a sequence of values ” ;
do
{ cin >> N;
A = A + N + B;
B ++;
} while (N < 10);
B *= 2;
cout << “(N,A,B) = ” << “(” << N << “, ” << A << “, ” << B << “)” <<endl;
return 0;
5. Show the output for the following program:
#include<iostream>
using namespace std;
int main()
{ int I, J;
for (I=1; I<4; ++I)
{ for (J=4; J>1; —J)
{ cout << J << “, ” << I;
cout << endl;
}
}
return 0;
6. Show the output of the following program using the input data: 8 -3 20
#include<iostream>
using namespace std;
int main()
{ int N, R, S;
cout << “Enter a list of integers terminated by 20.”;
S=1;
R=0;
do
{
cin >> N;
R++;
S = S + N;
} while (N < 20);
cout << “S = ” << S << “ R = “ << R;
return 0;
Engineering Problem Solving with C++, 3e Chapter 4 Test Bank
7. Write a short C++ program to read positive integers, until the value -1 is read and print the
sum of the values.
8. Which control statement which is best used when you know how many times to repeat the
execution of a group of statements?
9. The control statement which is best used when you need to select from among many integer
choices is the . . .
10. The control statement which is best used when you want to repeat a group of statements
based on a condition and the statements in the loop body must be executed at least once is the
. . .
11. When more than one statement appears in a loop body . . .
Use the following program to answer the next two questions
#include<iostream>
using namespace std;
int main()
{ int I, J;
for (I=2; I>=0; —I)
{ for (J=1; J<4; ++J)
cout << J << “, ” << I << “, “;
cout << endl;
}
return 0;
}
13. How many times is the statement cout << endl: executed?
15. A do-while loop repeats the loop body zero or more times.
16. One reason for an infinite loop in a while loop is that the loop body has no statement which
18. The continue statement causes the program to skip the rest of the loop body and to determine
19. A while loop repeats the loop body zero or more times.
21. When multiple initialization and/or modification statements are required in a for statement,
23. Sentinel-controlled input loops require a priori knowledge of how many data are in the file.
25. eof-controlled input loops require a priori knowledge of how many data are in the file.