Problem 12
Consider the following function: int test(int x, int y) { if (x == y) return x; else if (x > y) return (x + y); else return test(x + 1, y - 1); } What is the output of the following statements? a. cout << test(5, 10) << endl; b. cout << test(3, 9) << endl;
Problem 13
Consider the following function: int func(int x) { if (x == 0) return 2; else if (x == 1) return 3; else return (func(x - 1) + func(x - 2)); } What is the output of the following statements? a. cout << func(0) << endl; b. cout << func(1) << endl; c. cout << func(2) << endl; d. cout << func(5) << endl;
Problem 14
Suppose that intArray is an array of integers, and length specifies the number of elements in intArray. Also, suppose that low and high are two integers such that 0 <= low < length, 0 <= high < length, and low < high. That is, low and high are two indices in intArray. Write a recursive definition that reverses the elements in intArray between low and high.
Problem 15
Write a recursive algorithm to multiply two positive integers m and n using repeated addition. Specify the base case and the recursive case.
Problem 16
Consider the following problem: How many ways can a committee of four people be selected from a group of 10 people? There are many other similar problems in which you are asked to find the number of ways to select a set of items from a given set of items. The general problem can be stated as follows: Find the number of ways r different things can be chosen from a set of n items, in which r and n are nonnegative integers and r n. Suppose C(n, r) denotes the number of ways r different things can be chosen from a set of n items. Then, C(n, r) is given by the following formula: Cðn;rÞ ¼ n! r!ðn rÞ! in which the exclamation point denotes the factorial function. Moreover, C(n, 0) = C(n, n) = 1. It is also known that C(n, r) = C(n – 1, r – 1) + C(n – 1, r). a. Write a recursive algorithm to determine C(n, r). Identify the base case(s) and the general case(s). b. Using your recursive algorithm, determine C(5, 3) and C(9, 4).