Comp 1572, Winter 2002 Pencil-and-Paper Assignment 2 Assigned Friday, January 11, 2002 Due Friday, January 18, 2002 (At the beginning of class) ============================================================================ Solutions ============================================================================ 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 f ANS: cout << f[6] << endl; b) Input a value into element 4 of floating-point array b. ANS: The question is ambiguous; do they mean the element numbered 4 or do they mean the fourth element? If they meant the former, then cin >> b[4]; otherwise cin >> b[4]; c) Initialize each of the 5 elements of single-subscripted integer array g to 8. ANS: int g[5] = {8,8,8,8,8}; or for (int j = 0; j < 5; j++) g[j] = 8; d) Total and print the elements of floating-point array c of 100 elements. ANS: for (int k = 0; k < 100; k++) { total += c[k] // assume total is declared and initialized to 0 cout << c[k] << endl; } cout << "Total is " << total; e) Copy array a into the first portion of array b. Assume double a[ 11 ], b[ 34 ]; ANS: for (int i = 0; i <= 10; i++) b[i] = a[i]; f) Determine and print the smallest and largest values contained in 99-element floating-point array w. ANS: // assume all variables declared and initialized // in particular, initialize smallest to w[0] for (int j = 0; j < 99; j++) { if (w[j] < smallest) smallest = w[j]; if (w[j] > largest) largest = w[j]; } cout << "Largest value is " << largest << endl; cout << "Smallest value is " << smallest << endl; 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 ANS: We need a slot at the end of the array for the null character, so we need 6 slots - not enough space in the variables str b) Assume that: int a[ 3 ]; cout << a[ 1 ] << " " << a[ 2 ] << " " << a[ 3 ] << endl; ANS: If they meant to print the three numbers in the array, then they should have used a[0], a[1], a[2] - array subscripts begin at 0. c) double f[ 3 ] = { 1.1, 10.01, 100.001, 1000.0001 }; ANS: Too many initializers - at most 3 if they've declared f[3] 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. ANS: This solution assumes that the integers is positive int reverse_digits(int num) { int ans(0); while (num > 0) { ans = ans*10+num%10; num /= 10; } return(ans); } 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. ANS: #include using namespace std; float array_average(float [], int); int main() { float a[5]; // store input from the user here // input 5 floating point numbers from user for (int i=0; i < 5; i++) { cout << "Please enter a number: "; cin >> a[i]; } // calculate average of 5 numbers and display it cout << array_average(a, 5); } // Calculate the average of the elements of b // len gives the number of numbers in the array float array_average(float b[], int len) { float total(0); for (int i=0; i < len; i++) total += b[i]; return total/len; } 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). ANS: void shift_array(int arr[], int len) { for (int i = 0; i < len-1; i++) arr[i] = arr[i+1]; } 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. ANS: Assume that exp is a positive integer, notice that base can be negative int integer_power(int base, int exp) { if (exp==1) return base; else return base*integer_power(base, exp-1); } 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); ANS: bool isPalindrome(string w) { if (w.length() == 0 || w.length()==1) // if it's empty or 1 char long return true; // it's a palindrome else if (w.at(0) != w.at(w.length()-1)) // if first and last don't match return false; // it's not a palindrome // Now first and last are the same, so strip them off the string // and check that what remains is a palindrome: else return isPalindrome(w.substr(1, w.length()-2)); }