CHAPTER 5 (16-33, 34-70) REVIEW QUESTIONS Flashcards
For Exam 2
- To __________ a value means to increase it by one, and to __________ a value means to decrease it by one.
- increment, decrement
- When the increment or decrement operator is placed before the operand (or to the operand’s left), the operator is being used in __________ mode.
- prefix
- When the increment or decrement operator is placed after the operand (or to the operand’s right), the operator is being used in __________ mode.
- postfix
- The statement or block that is repeated is known as the __________ of the loop.
- body
- Each repetition of a loop is known as a(n) __________.
- iteration
- A loop that evaluates its test expression before each repetition is a(n) __________ loop.
- pretest
- A loop that evaluates its test expression after each repetition is a(n) __________ loop.
- posttest
- A loop that does not have a way of stopping is a(n) __________ loop.
- infinite or endless
- A(n) __________ is a variable that “counts” the number of times a loop repeats.
- counter
- A(n) __________ is a sum of numbers that accumulates with each iteration of a loop.
- running total
- A(n) __________ is a variable that is initialized to some starting value, usually zero, and then has numbers added to it in each iteration of a loop.
- accumulator
- A(n) __________ is a special value that marks the end of a series of values.
- sentinel
- The __________ loop always iterates at least once.
- do-while
- The __________ and __________ loops will not iterate at all if their test expressions are false to start with.
- while and for
- The __________ loop is ideal for situations that require a counter.
- for
- Inside the for loop’s parentheses, the first expression is the __________ , the second expression is the __________ , and the third expression is the __________.
- initialization, test, update
- A loop that is inside another is called a(n) __________ loop.
- nested
- The __________ statement causes a loop to terminate immediately.
- break
- The __________ statement causes a loop to skip the remaining statements in the current iteration.
- continue
- T F The operand of the increment and decrement operators can be any valid mathematical expression.
- false
- T F The cout statement in the following program segment will display 5:
int x = 5;
cout «_space;x++;
- true
- T F The cout statement in the following program segment will display 5:
int x = 5;
cout «_space;++x;
- false
- T F The while loop is a pretest loop.
- true
- T F The do-while loop is a pretest loop.
- false
- T F The for loop is a posttest loop.
- false
- T F It is not necessary to initialize counter variables.
- false
- T F All three of the for loop’s expressions may be omitted.
- true
- T F One limitation of the for loop is that only one variable may be initialized in the initialization expression.
- false
- T F Variables may be defined inside the body of a loop.
- true
- T F A variable may be defined in the initialization expression of the for loop.
- true
- T F In a nested loop, the outer loop executes faster than the inner loop.
- false
- T F In a nested loop, the inner loop goes through all of its iterations for every single iteration of the outer loop.
- true
- T F To calculate the total number of iterations of a nested loop, add the number of iterations of all the loops.
- false
- T F The continue statement causes a terminated loop to resume.
- false
- T F The break statement causes a loop to stop the current iteration and begin the next one.
- false
- T F In a nested loop, the break statement only interrupts the loop it is placed in.
- true
- T F When you call an ofstream object’s open member function, the specified file will be erased if it already exists.
- true
Find the Errors. Each of the following programs has errors. Find as many as you can.
- // Find the error in this program.
#include <iostream>
using namespace std;
int main()
{
int num1 = 0, num2 = 10, result;
num1++;
result = ++(num1 + num2);
cout << num1 << " " << num2 << " " << result;
return 0;
}</iostream>
- The statement result = ++(num1 + num2); is invalid.
Find the Errors. Each of the following programs has errors. Find as many as you can.
- // This program adds two numbers entered by the user.
#include <iostream>
using namespace std;
int main()
{ int num1, num2;
char again;
while (again == 'y' || again == 'Y')
cout << "Enter a number: ";
cin >> num1;
cout << "Enter another number: ";
cin >> num2;
cout << "Their sum is << (num1 + num2) << endl;
cout << "Do you want to do this again? ";
cin >> again;
return 0;
}</iostream>
- The while loop tests the variable again before any values are stored in it.
The while loop is missing its opening and closing braces.
Find the Errors. Each of the following programs has errors. Find as many as you can.
- // This program uses a loop to raise a number to a power.
#include <iostream>
using namespace std;
int main()
{
int num, bigNum, power, count;
cout << "Enter an integer: ";
cin >> num;
cout << "What power do you want it raised to? ";
cin >> power;
bigNum = num;
while (count++ < power);
bigNum *= num;
cout << "The result is << bigNum << endl;
return 0;
}</iostream>
- The while statement should not end with a semicolon.
It could also be argued that bigNum should be defined a long.
count should be initialized to 1.
Find the Errors. Each of the following programs has errors. Find as many as you can.
- // This program averages a set of numbers.
#include <iostream>
using namespace std;
int main()
{
int numCount, total;
double average;
cout << "How many numbers do you want to average? ";
cin >> numCount;
for (int count = 0; count < numCount; count++)
{
int num;
cout << "Enter a number: ";
cin >> num;
total += num;
count++;
}
average = total / numCount;
cout << "The average is << average << endl;
return 0;
}</iostream>
The variable total is not initialized to 0.
The statement that calculates the average performs integer division. It should use a type cast to cast either total or numCount to a double. The variable count is incremented in the for loop's update expression and again within the for loop.
Find the Errors. Each of the following programs has errors. Find as many as you can.
- // This program displays the sum of two numbers.
#include <iostream>
using namespace std;
int main()
{
int choice, num1, num2;
do
{
cout << "Enter a number: ";
cin >> num1;
cout << "Enter another number: ";
cin >> num2;
cout << "Their sum is " << (num1 + num2) << endl;
cout << "Do you want to do this again?\n";
cout << "1 = yes, 0 = no\n";
cin >> choice;
} while (choice = 1)
return 0;
}</iostream>
The expression tested by the do-while loop should be choice == 1 instead of choice = 1.
Find the Errors. Each of the following programs has errors. Find as many as you can.
- // This program displays the sum of the numbers 1-100.
#include <iostream>
using namespace std;
int main()
{
int count = 1, total;
while (count <= 100)
total += count;
cout << "The sum of the numbers 1-100 is ";
cout << total << endl;
return 0;
}</iostream>
The variable total is not initialized to 0.
The while loop does not change the value of count, so it iterates an infinite number of times.