CS 1044 Homework 3 Key Spring 1999 Q Ans Reason 1 4 The loop condition is loopCount <= 145, and loopCount is incremented by one on each iteration, so the loop will exit when loopCount reaches 146. 2 2 The loop condition is initially false, so you never enter the while loop, and someInt is never changed. 3 4 The loop continues as long as beta > 0 AND beta < 10. The loop will terminate when the loop condition is false. That will be when beta > 0 is false or beta < 10 is false, or both. Now beta > 1 is false if beta <= 1; beta < 10 is false if beta >= 10. So the loop termination condition is beta <= 1 OR beta >= 10. 4 1 Just trace it by hand. 5 4 Again, just trace by hand. This differs from 4 in that n is incremented before it's printed here. 6 3 There aren't any curly braces, so the body of the while loop is just the cout statement. So, the while loop doesn't contain a statement that will ever modify the value of n. So, the while loop will never terminate. 7 5 A sentinel value should be chosen so that it isn't a logically possible value for the variable in question. A voter who is 999 years old is very unlikely; month numbers should run from 1 through 12 (or 0 through 11). So those are acceptable sentinel values. But, a test score certainly could equal 1 (although we'd hope not), and the speed of a car could certainly equal 75, sothose are poor choices for sentinel values. 8 4 9 1 The priming read must go before the beginning of the loop. 10 3 Just trace it by hand; this one is a bit complicated though: Count Length 4 5 // initial values 4 20 // first iteration, execute if..else 5 20 // increment Count 5 100 // second iteration, execute if..else 6 100 // increment Count 6 98 // third iteration, execute if..else 7 98 // increment Count 11 5 The semicolon after the while loop condition is a null statement, and so that is the while loop body. The compound statement isn't inside the loop. So, there is nothing within the while loop to ever modify the value of loopCount, and so the while loop is infinite. 12 3 Trace it by hand: Outer loop Inner Loop outerCount innerCount Sum 1 ? 0 // initial values 1 0 // first iteration 1 1 2 1 1 // first iteration 1 2 2 2 // second iteration 2 4 3 1 4 // first iteration 1 5 2 7 // second iteration 3 10 13 2 The do-loop body is executed only one time since loopCount will be 11 after that iteration, and that makes loopCount < Limit false. 14 3 The loop must exit as soon as the value 8 is printed for n; the only condition that will make that happen is n < 8. 15 1 The while loop sums the integers from -5 to 15 (inclusive). The first for loop does the same thing. The second and third for loops increment the loop counter twice, which will cause some summations to be missed. The fourth for loop sums 1 through 21, which yields a different result. 16 4 The given for loop makes 10 iterations (i going from 1 to 10). For loop (1) and for loop (2) do the same, although (1) counts down instead of up. For loop 3 does 11 iterations. 17 3 Just trace it. 18 1 Again, just trace it. In this case, the for loop condition is false initially. 19 3 This is similar to question 11; the for loop body is the null statement because of the semicolon following the for header. So, the for loop will execute 3 iterations, but none of those will print anything. 20 2 On the first iteration of the for loop, the while loop will assign n the value 8. Since that makes the while condition false, none of the remaining iterations of the for loop will change n.