Comp 1572, Winter 2002 Pencil-and-Paper Assignment 1 Assigned January 4, 2002 Due Wednesday, January 9, 2002 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 ); 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; 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; } c) The following code should output the odd integers from 19 to 1: for ( x = 19; x >= 1; x += 2 ) cout << x << endl; d) The following code should output the even integers from 2 to 100: counter = 2; do { cout << counter << endl; counter += 2; } While ( counter < 100 ); 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"; 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; 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's 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; } 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; } 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; } 8. This is problem 3.28 from Deitel and Deitel: Write a function that returns the smallest of three double-precision, floating-point numbers. 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. 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.