Homework 4 Due Tuesday, October 8, 2002 Solutions, updated with corrections to g, 1e, 2d, and a) response_received | non_interactive | (response_received || non_interactive) ---------------------------------------------------------------------------- T | T | T T | F | T F | T | T F | F | F b) already_made_mistake | patience | !(already_made_mistake && !patience) ---------------------------------------------------------------------- T | T | T T | F | F F | T | T F | F | T c) already_made_mistake | patience | (!already_made_mistake || patience) --------------------------------------------------------------------- T | T | T T | F | F F | T | T F | F | T d) switch (value % 2) { case 0: cout << "Even integer" << endl; break; case 1: cout << "Odd integer" << endl; break; default: break; } e) There is a semicolon at the end of the while loop declaration. This will cause the while loop to be "empty" (nothing happens within the loop). Also, there is a missing open brace (alternately, an extra close brace). f) The angle brackets are facing the wrong direction for an input statement. The line should read: cin >> value; g) This is one set of code that works: int ctr = 1; while (ctr <= 20) { cout << ctr << " "; if (ctr % 5 == 0) cout << endl; ctr++; } Chapter 4 problems: 1a) False - Since logical expressions generate a 1 for true and 0 for false, the result can be stored in an int variable. 1b) False - A semicolon after an if statement will cause the action of the if statement to be empty, meaning the statements after the if will always be executed. 1c) False - Every if does not necessarily need a corresponding else, but every else needs a corresponding if. 1d) True - The programmer probably meant to say if (score==30), but given the error that they typed (score=30), the expression score=30 takes the value 30. This is nonzero, so the expression evaluates to true. The statement is ALWAYS executed. 1e) False - it should say evaluates to false if either ch < 'A' or ch > 'Z' 1f) False - a pair of braces is missing. The else therefore has no matching if. Therefore, the code does not compile. 1g) False - it should evaluate to an integer type 1h) False - The statement is also true if x equals 0. 1i) False - != is a relational operator, not a logical operator 1j) True 2d) (i), if you think the missing quote was a typo and you generously inserted it, or (iii) if you realized that the missing " causes the program to not compile 2e) (iii) 8) beta = 7 (3 + 3 = 6, then add 1 to get 7. Note that the programmer probably meant to put in break statements at the end of each case, but forgot. The programmer also forgot a default case) Chapter 5 problems: 1a) False - Counter-controlled while loops require that you initialize the control variable before you enter the loop. 1b) True - the body will not be executed if the expression is intially false 1c) False - In an infinite while loop, the control expression always evaluates to true (that's why the program never quits). 1d) True 1e) True 1f) True 1g) True 1h) False - When a while loop terminates, the control goes to the statement immediately following the while loop. 3) 5 4) 1 3 5 7 9 7) Sum = 158