5.4 Nested Loops
5.4.2 What is the output by the following code? int row; int col; for(row = 2; row <= 3; row = row + 1) { for(col = 0; col <= 1; col = col + 1) { cout << row << col << " "; } } ---------------------------------------------------------------------------- *The outer loop ranges from 2 to 3, and the inner loop ranges from 0 to 1. The first outer loop iteration outputs 20 and 21. The second outer loop iteration outputs 30 and 31.*
20, 21, 30, 31
5.4.1 #include <iostream> using namespace std; int main() { int numAsterisk; // Number of asterisks to print int i; // Loop counter numAsterisk = 0; while (numAsterisk >= 0) { cout << "Enter an integer (negative to quit): "; cin >> numAsterisk; if (numAsterisk >= 0) { cout << "Depicted graphically:" << endl; for (i = 1; i <= numAsterisk; ++i) { cout << "*"; } cout << endl << endl; } } cout << "Goodbye." << endl; return 0; } RESULT: Enter an integer (negative to quit): 9 Depicted graphically: ******************* Enter an integer (negative to quit): 23 Depicted graphically: ************************ Enter an integer (negative to quit): 35 Depicted graphically: *********************************** Enter an integer (negative to quit): -1 Goodbye. __________________________________________________________________________________________________ Given the following code, how many times will the inner loop body execute? char letter1; char letter2; letter1 = 'a'; while (letter1 <= 'f') { letter2 = 'c'; while (letter2 <= 'f') { // Inner loop body ++letter2; } ++letter1; } =====================================================================
24
5.4.1 #include <iostream> using namespace std; int main() { int numAsterisk; // Number of asterisks to print int i; // Loop counter numAsterisk = 0; while (numAsterisk >= 0) { cout << "Enter an integer (negative to quit): "; cin >> numAsterisk; if (numAsterisk >= 0) { cout << "Depicted graphically:" << endl; for (i = 1; i <= numAsterisk; ++i) { cout << "*"; } cout << endl << endl; } } cout << "Goodbye." << endl; return 0; } RESULT: Enter an integer (negative to quit): 9 Depicted graphically: ****************************************** Enter an integer (negative to quit): 23 Depicted graphically: *********************** Enter an integer (negative to quit): 35 Depicted graphically: *********************************** Enter an integer (negative to quit): -1 Goodbye. __________________________________________________________________________________________________ Given the following code, how many times will the inner loop body execute? int row; int col; for(row = 0; row < 2; row = row + 1) { for(col = 0; col < 3; col = col + 1) { // Inner loop body } } =====================================================================
6
5.5.1 A good programming process is to write the entire program, then incrementally remove bugs one at a time. TRUE OR FALSE
False
5.5.1 Expert programmers need not develop programs incrementally. TRUE OR FALSE
False
5.5.1 Once a program is complete, one would expect to see several FIXME comments.
False
a loop that appears in the body of another loop.
Nested loop
5.5.1 FIXME comments provide a way for a programmer to remember what needs to be added. TRUE OR FALSE
True
5.5.1 Incremental programming may help reduce the number of errors in a program. TRUE OR FALSE
True
5.4.2 What is output by the following code? char letter1; char letter2; letter1 = 'y'; while (letter1 <= 'z') { letter2 = 'a'; while (letter2 <= 'c') { cout << letter1 << letter2 << " "; ++letter2; } ++letter1; } ===================================================================== The outer loop ranges from y to z, and the inner loop ranges from a to c. First outer loop iteration prints y_ y_ y_. Second prints z_ z_ z_.
ya yb yc za zb zc