Comp 1572, Winter 2002 Pencil-and-Paper Assignment 2 Assigned Friday, January 11, 2002 Due Friday, January 18, 2002 (At the beginning of class) ============================================================================ This is pencil-and-paper homework. Write your answers as neatly as possible. If your handwriting is hard to read, please type your answers. ============================================================================ 1. Problem 4.8 from Deitel and Deitel: Write C++ statements to accomplish each of the following: a) Display the value of the seventh element of character array b) Input a value into element 4 of single-subscripted floating-point array b. c) Initialize each of the 5 elements of single-subscripted integer array g to 8. d) Total and print the elements of floating-point array c of 100 elements. e) Copy array a into the first portion of array b. Assume double a[ 11 ], b[ 34 ]; f) Determine and print the smallest and largest values contained in 99-element floating-point array w. 2. Problem 4.13(a)-(c) from Deitel and Deitel: Find the error(s) in each of the following statements: a) Assume that: char str[ 5 ]; cin >> str; // User types hello b) Assume that: int a[ 3 ]; cout << a[ 1 ] << " " << a[ 2 ] << " " << a[ 3 ] << endl; c) double f[ 3 ] = { 1.1, 10.01, 100.001, 1000.0001 }; 3. Problem 3.31 from Deitel and Deitel: Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367. 4. Write a function that takes as input an array of floats and the length of the array and returns the average (arithmetic mean) of the numbers in the array. Use this function to write a program that inputs 5 numbers from the user, then prints them out on one line, and prints their average on the next line. 5. Write a function that takes an array of integers and the length of the array as parameters, then modifies the array by getting rid of the first element (the other numbers will have to shift forwards). 6. Problem 3.40 from Deitel and Deitel: Write a recursive function power( base, exponent ) that, when invoked, returns the value of base^exponent. For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. Hint: The recursion step would use the relationship base^exponent = base x base^(exponent - 1) and the terminating condition occurs when exponent is equal to 1 because base^1 = base. 7. A palindrome is a string that is spelled the same forward and backward (e.g., "mom" and "racecar"). Its recursive structure is as follows: Let c be a single character, and w be a string. -- Basis: string cc is a palindrome. a string of a single character c is a palindrome. -- Recursive step: if string w is a palindrome, then cwc is a palindrome. Given the function prototype below, write a recursive function isPalindrome that returns true if a string is a palindrome. bool isPalindrome(string w);