Comp 1572, Winter 2002 Pencil-and-Paper Assignment 1 Assigned January 4, 2002 Due Wednesday, January 9, 2002 ========= SOLUTIONS ========= 1. This is problem 2.34 from Deitel and Deitel: What's wrong with the following statement? Provide the correct statement to accomplish what the programmer was probably trying to do. cout << ++( x + y ); ANS: ++var; has the same effect as var = var + 1; (and the value of the expression is the value of var *after* the increment). Thus, var must be an lvalue. (x+y) is not an lvalue - in other words, there is no place to "store" the result of the increment, so ++(x+y) doesn't make sense. The programmer probably meant cout << x+y+1; 2. This is problem 2.39 from Deitel and Deitel: Find the error(s) in each of the following: a) For ( x = 100, x >= 1, x++ ) cout << x << endl; ANS: For should be for. The commas should be semicolons. The ++ should be a decrement such as --, to prevent an infinite loop. b) The following code should print whether integer value is odd or even: switch ( value % 2 ) { case 0: cout << "Even integer" << endl; case 1: cout << "Odd integer" << endl; } ANS: case 0 needs a break statement. c) The following code should output the odd integers from 19 to 1: for ( x = 19; x >= 1; x += 2 ) cout << x << endl; ANS: += should be -=. d) The following code should output the even integers from 2 to 100: counter = 2; do { cout << counter << endl; counter += 2; } While ( counter < 100 ); ANS: While should be while. Operator < should be <=. 3. How many Xs does this program segment print? Try predicting using your understanding of the for loop. You can check your answer on a computer so you know you're right. for (int x = 0; x < 10; x++) for (int y = 5; y > 0; y--) cout <<"X"; ANS: The "inner" loop (the second for statement) prints out 5 Xs. But this inner loop is the body of the outer loop (the first for statement). The outer loop runs 10 times, and each time it runs, the inner loop prints out 5 Xs. So you see 50 Xs. 4. What value is printed by the code segment below? First predict using your understanding of the for loop. You can check your answer on a computer to make sure you're right. int ctr; for (ctr = 2; ctr < 10; ctr += 3) ; cout << ctr; ANS: This for loop has an empty body, so all of the computation happens in the loop control. The variable ctr is initialized to 2, which is less than 10, so the increment is executed, and ctr is incremented by 3 to 5. Again, it's less than 10, so it's incremented by 3 again to 8. This value is still less than 10, so it's incremented to 11, at which point the continuation condition is not met and we exit the for loop. The value printed is 11. 5. The following function is supposed to ask the user to type 1 or 2 repeating the question until the user actually enters 1 or 2. Then it should return the users' answer as the function's value. But it doesn't work. Correct the function, and if I were you, I'd be sure to test your result on the computer. Either turn in a printout of the corrected function, or just give the line(s) that have changed with an explanation. int get_1_or_2( ) { int answer = 0; While (answer < 1 && answer => 2) { cout << "Enter 1 for Yes, 2 for No"; cin >> answer; } return answer; } ANS: First, the While should be while. Also, the condition in the while() was incorrect. There is no single number that is both <1 and >=2. One way to fix the problem is to change the while statment to while (answer != 1 && answer != 2) 6. What's wrong with the following program? Fix it so that it behaves reasonably, explain what line(s) you changed and how. #include int main() { int x = 0: if( x = 1); cout<< " x equals 1"; otherwise cout<< " x does not equal 1"; return 0; } ANS: The : should be ; in the definition of x. The condition if (x=1) actually assigns a value to x instead of testing it. The semicolon after the if() shouldn't be there (it makes the body of the if empty). The "otherwise" should be "else". 7. The logic in this program is fine, but there are some syntax errors. Fix the errors so that the program runs. Describe the changes. #include int main() { unsigned int beer; cout >> "How many bottles do you have? "; cin << beer; while (beer > 0) { cout >> beer >> " bottles of beer on the wall, " >> beer >> " bottles of beer >> endl"; cout >> "Take one down, pass it around >> endl"; beer--; cout >> beer >> " bottles of beer on the wall. >> endl"; } return 0; } ANS: All of the >> should be replaced with <<. On the last three cout statement, the last " should be moved to before the stream insertion character. 8. This is problem 3.28 from Deitel and Deitel: Write a function that returns the smallest of three double-precision, floating-point numbers. ANS: double smallest3( double a, double b, double c ) { if ( a < b && a < c) return a; else if ( b < c) return b; else return c; } 9. This is problem 3.18 from Deitel and Deitel Write a function integerPower(base,exponent) that returns the value of base to the power exponent. For example, integerPower(3,4) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero integer and that base is an integer. The function integerPower should use for or while to control the calculation. Do not use any math library functions. ANS: int integerPower( int b, int e ) { int product = 1; for ( int i = 1; i <= e; ++i ) product *= b; return product; } 10. Write a function IsLeapYear that takes an integer year and returns a bool. It should, of course, return true if the year is a leap year, and false otherwise. You'll need to use the modulus operator % in this problem. FYI - leap years come every fourth year (years divisible by 4), except every century (e.g, 1800 or 1900), unless the century is divisible by 400. ANS: There are many ways to do this. Here are two: bool IsLeapYear(int year) { if (year%4) return false; /* not a multiple of 4 - definitely not a leap year */ else if (year%100) return true; /* Is a mult. of 4, but not a century year, - yes! */ else if (year%400) return false; /* A century year, but not a mult. of 400 -no good! */ else return true; /* All that's left is multiples of 400 - leap years */ } -or - bool IsLeapYear(int year) { return(!(year%4) && !(!(year%100) && year%400)); }