CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion Consider the following recursive function: int what ( int x, int y) if (x > y) return what (x-y, y); else if (y > x) return what (x, y-x); else return x; Trace the above function for the following calls: what (104,16) what (15,35) what (63,18) What is the objective of the above function? Write a recursive Boolean function palindrome ( s, first, last ) to return true if the string s is a palindrome, that is, the string reads the same backwards as forwards. Examples are dad and bob. The parameters first, last are the indices of the first and last elements of the part of the string being checked, respectively. Validate your function using examples of your choice. Given the following array a[ ] of integers: 5 3 0 17 10 9 20 0 1 2 3 7 Trace the function call m = process(a, 12, 10 ) to determine the value of m and the final contents of a[ ] returned by the following recursive function: int process (int a[ ], int n, int x ) if ((x < 0) (x > n-1)) return 0; else if (a[x] == 0) return 0; else a[x] = 0; return (1 + process(a, n, x-1) + process(a, n, x+1));
Consider the following recursive function : long int Func ( int n, int m ) if ((m == 0) (m == n)) return 1 ; else return ( Func ( n-1, m) + Func( n-1, m-1) ) ; If long int z = Func( 5, 3 ); trace the above function to determine the value of z. Consider an array a[ ] of non-zero integers. Implement a recursive function sum (a[ ], s, e) based on the Divide & Conquer method to return the sum of all the elements in the array between index (s) and index (e) inclusive (use division at the approximate middle). Test your function by tracing it for the call sum (a, 0, 6) given the following array: 2 4 1 8 4 8 2 Consider the following main function that invokes the recursive function value. Trace the function call to determine the final contents of the array a[ ]. int main( ) int a[6] = 5, 7, 12, 3, 14, 3 ; int k = 1; for ( int i = 0; i < 6; i++) a[ i ] = value (k) / (k*k); k *= 2; int value ( int n ) if ( n < 2 ) return 0; else return ( 4 * value (n / 2) + n * n ); Answer: i k value(k) a[i]
Write a recursive function oddcount ( int a[ ], int s, int e) to receive an integer array a [ ] a start index (s), and an end index (e) and return the number of odd integers in that array between s and e. Write a recursive function that receives a string of characters and returns the accumulating sum of the ASCII values of the characters in the string, excluding blanks from the sum. Write a recursive function to return the number of zeros in an array A of integers of size n Write a recursive boolean function to return true if an integer x is found in the first n elements of an array of integers A, and false otherwise. Write a recursive function Sum(int X[ ], int N) to return the sum of the values in an integer array X with subscripts from 1 to N. Let x be positive real. To calculate the square root of x by Newton's Method, we start with an initial approximation a = x/2. If abs (a*a-x) < epsilon, we stop and return with the result (a). Otherwise, we replace (a) by (a + x/a) / 2 and repeat the process until we find an approximation close enough to stop. Write a recursive function Newton (x, a, epsilon) to compute the square root of x using the above method and taking epsilon = 10-6. Write a function Square_Root(x) to call the above function and return the final result of the square root of x. Assume that an array a[ ] of characters of size N = 7 has the following contents: O N E T W O Trace the following functions to show the output of the calls Show1(a, 0, N-1) and Show2(a, 0, N-1) void Show1 ( char a[ ], int s, int e) if (s < = e) int m = (s + e)/2; cout << a[m]; Show1 ( a, s, m-1); Show1 ( a, m+1, e); void Show2 ( char a[ ], int s, int e) if (s < = e) int m = (s + e)/2; Show2 ( a, s, m-1); cout << a[m]; Show2 ( a, m+1, e);
The Fibonacci numbers can be computed using the following recursive function: int Fib(int n) if (n < 2) return 1; else return Fib(n-2) + Fib(n-1); What is the value of Fib ( 8 )? How many recursive calls are needed to compute Fib( 8 )? The Binomial Coefficient is also the number of combinations of (m) things out of (n) things. It may be written as: n n 1 n 1 comb( n, m), m n m m 1 m with comb( n,0) comb( n, n) 1 This leads to the recursive function: int comb (int n, int m) if ((m == 0) (m == n)) return1; else return comb (n 1, m 1) + comb (n 1, m); What is the value of comb ( 6, 2 ) and how many calls are needed? Ackermann s function A(m,n) is defined as : n + 1 if m = 0 A(m,n) = A(m-1, 1) if n = 0 A(m-1, A(m, n-1)) otherwise What is the value of A(2,1)? How many recursive steps are used in this case?
Given a global array x[0..n-1] of integers and the following recursive function: void Ruler ( int L, int R, int h ); m = ( L + R ) / 2 ; If ( h > 0 ) x[m] = h ; Ruler ( L, m, h-1) ; Ruler (m, R, h-1 ) ; Trace the above algorithm with the call Ruler ( 0, 8, 3 ) and show the resulting first 7 elements of the array. Draw vertical lines with heights proportional to the values in the array locations. What do these lines represent?