CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions) 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? X=104 X=88 what (104,16) X=72 X=56 X=40 X=24 X=8 y=8 X=8 what (15,35) X=15 y=35 X=15 y=20 X=15 y=5 X=10 y=5 X=5 y=5 what (63,18) X=63 X=45 X=27 X=9 X=9 y=9 The function finds the GCD for two integers This is the recursive algorithm for the GCD (Greatest common Divisor of two integers). 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. bool palindrome(string s, int first, int last) if (first>=last) return true; else if(s[first]!= s[last]) return false; else return palindrome(s, first1, last-1); dad true Tracing Palindrome dad and daa true F=0, L=2 F=1, L=1 dabd false F=0, L=3 false F=1, L=2
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, x1)); m = process (a, 12, 10 ); 0 5 3 0 17 10 9 20 0 1 2 3 7 X=8 A[8] = 0 X=7 X=9 0 0 X=9 A[9] = 0 0 n=12, x=10 A[10] = 0 X=11 A[11] = 0 10 0 0 X=10 X=10 X=12 m = 4 This algorithm will return the size of the non-zero sequence of elements of which the element at x is a member. It will replace these elements by zeros. Hence, the size of the non-zero sequence of elements of which element at location 10 is a member will be 4 (values 1, 2, 3, 7). The final array will be: 5 3 0 17 10 9 20 0 0 0 0 0
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. long int z = Func( 5, 3 ); z=10 4 n=5, m=3 6 n=4, m=3 n=4, m=2 1 3 3 3 n=3, m=3 n=3, m=2 n=3, m=2 n=3, m=1 1 2 1 2 2 1 n=2, m=2 n=2, m=1 n=2, m=2 n=2, m=1 n=2, m=1 n=2, m=0 1 1 1 1 1 1 1,1 1,0 1,1 1,0 1,1 1,0 This is the recursive algorithm for finding the number of combinations of n objects taken m at a time (Binomial Coefficient). 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
int sum ( int a[ ], int s, int e) if ( s > e ) return 0; else if ( s == e ) return a [ s ]; else int m = ( s e ) / 2; return sum ( a, s, m ) sum ( a, m1, e ); Tracing sum (a, 0, 6): result is the sum of elements in array = 29 sum (Divide & Conquer) 15 s=0, e=6 M=3 29 14 s=0, e=3 M=1 s=4, e=6 M=5 A[0] s=0,e=0 6 s=0, e=1 M=0 A[1] A[2] 9 s=2, e=3 M=2 A[3] A[4] s=4, e=5 M=4 12 A[6] A[5] s=1,e=1 s=2,e=2 s=3,e=3 s=4,e=4 s=5,e=5 s=6, e=6 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. int oddcount(int a[ ], int s, int e) if (s > e) return 0; else return ( a[s]%2 ) oddcount (a, s1, 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. //Blanks Included int asciisum (string s, int first, int last) if( first > last) return 0; return int ( s [first] ) asciisum (s, first 1, last); // Blanks Excluded int asciisum (string s, int first, int last) if( first > last) return 0; if( s[first] == ) return asciisum (s, first 1, last); return int ( s [first] ) asciisum (s, first 1, last);
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. //Recursive Sequential Search of x in array A from location s through location e. bool LinSearch (int A[ ], int x, int s, int e) if (x == A[s]) return true; else if (s == e) return false; else return LinSearch (A,x,s1,e); Call this function as: LinSearch(A, x, 0, n-1); 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, m1, 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, m1, e);