Comp 1672, Winter 2003 Homework Assignment 1 Assigned January 6, 2003 Due Friday, January 10, 2003 Due date extended to Tuesday, January 14, 2003 1. Find and fix the error(s) in each of the following. The errors include logic errors and syntax errors. a) Since x is initialized to 0, this program should output "x does not equal 1". But that's not what happens! int main() { int x = 0: if (x = 1) cout<< " x equals 1"; otherwise cout<< " x does not equal 1"; return 0; } b) For ( x = 100, x >= 1, x++ ) cout << x << endl; c) 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; } d) The following function should get a 0 or a 1 from the user, and return which one it successfully input. int get_0_or_1() { int answer = -1; while (answer < 0 && answer => 1) { cout << "Enter 1 for Yes, 0 for No"; cin >> answer; } return answer; } 2. How many Xs does this program segment print? Try predicting using your understanding of the for loop, before you check your answer on a computer. int i, j; for (i = 0; i < 5; i++) for (j = 5; j>=i ; j--) cout <<"X"; 3. What value is printed by the code segment below? You can use a computer to check your answer to make sure you're right. This is a trick question. int ctr; for (ctr = 5; ctr < 20; ctr += 3); cout << ctr; 4. Write a function called power that takes two integers (call them base and exponent), calculates base to the power exponent, and returns that value. For example, power(2, 5) returns 2*2*2*2*2, or 32. Assume that exponent is positive. Use a loop structure - don't use any library functions. 5. The Fibonacci sequence is a sequence of numbers that starts with 1, 1, and each subsequent element is found by adding the previous two numbers. The third number in the sequence is thus 1+1 = 2. Following this same rule, here are the first few elements: 1, 1, 2, 3, 5, 8, 13, 21, 34 ... Write a function called fibonacci that takes an integer representing which term in the sequence we want, and returns that element of the Fibonacci sequence. For example, fibonacci(3) returns 2, and fibonacci(8) returns 21.