charpter5 Flashcards
5.1 What will the following program segments display?
A) x = 2;
y = x++;
cout
A) 32 B) 33 C) 23 D) 34 E) It is true! F) It is true!
5.2 Write an input validation loop that asks the user to enter a number in the range of
10 through 25.
int number;
cout «_space;“Enter a number in the range 10 through 25: “;
cin»_space; number;
while (number < 10 || number > 25)
{
cout «_space;“Error! The number must be in the range “
«_space;“of 10 through 25. Enter a valid number: “;
cin»_space; number;
}
5.3 Write an input validation loop that asks the user to enter Y , y , N , or n .
char letter; cout << "Enter Y for yes or N for no: "; cin >> letter; while (letter != 'Y' && letter != 'y' && letter != 'N' && letter != 'n') { cout << "Error! Enter either Y or N: "; cin >> letter; }
5.4 Write an input validation loop that asks the user to enter Yes or No .
string input; cout << "Enter Yes or No: "; cin >> input; while ( (input != "Yes") && (input != "No") ) { cout << "Error! Enter either Yes or No: "; cin >> input; }
5.5 What will the following program segments display? A) int count = 10; do { cout
A) Hello World
B) 10
C) 8 4
5.6 Name the three expressions that appear inside the parentheses in the for loop s
header.
Initialization, test, and update.
You want to write a for loop that displays I love to program 50 times. Assume
that you will use a counter variable named count.
A) What initialization expression will you use?
B) What test expression will you use?
C) What update expression will you use?
D) Write the loop.
A) int count = 1;
B) count
5.8 What will the following program segments display?
A) for (int count = 0; count
A) 0246810 B) -5-4-3-2-101234 C) 5 8 11 14 17
5.9 Write a for loop that displays your name 10 times.
for (int count = 1; count «_space;“Chris Coder\n”;
5.10 Write a for loop that displays all of the odd numbers, 1 through 49.
for (int count = 1; count «_space;count «_space;endl;
5.11 Write a for loop that displays every fth number, zero through 100.
for (int count = 0; count «_space;count «_space;endl;
5.12 Write a for loop that repeats seven times, asking the user to enter a number. The
loop should also calculate the sum of the numbers entered.
int number, total = 0; for (int count = 0; count < 7; count++) { cout << "Enter a number: "; cin >> number; total += number; }
5.13 In the following program segment, which variable is the counter variable and which is the accumulator? int a, x, y = 0; for (x = 0; x > a; y += a; } cout
5.13 The variable x is the counter variable. The variable y is the accumulator
5.14 Why should you be careful when choosing a sentinel value?
You must be sure to choose a sentinel value that could not be mistaken as a member of the
data list.
5.15 How would you modify Program 5-13 so any negative value is a sentinel?
5.15 Change the while loop to read:
while (points >= 0)