1 / 22

Exercise 3

Exercise 3. CS 110 S12. 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)

zariel
Download Presentation

Exercise 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exercise 3 CS 110 S12

  2. 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?

  3. what (104,16) X=104 y=16 X=88 y=16 X=72 y=16 X=56 y=16 X=40 y=16 X=24 y=16 X=8 y=16 X=8 y=8

  4. what (15,35) X=15 y=35 X=15 y=20 X=15 y=5 X=10 y=5 X=5 y=5

  5. what (63,18) X=63 y=18 X=45 y=18 X=27 y=18 X=9 y=18 X=9 y=9 The function finds the GCD for two integers

  6. 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.

  7. Palindrome 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, first+1, last-1); }

  8. Tracing Palindrome “dad” and “daa” “dad” “dabd” true false F=0, L=2 F=0, L=3 true false F=1, L=2 F=1, L=1

  9. Given the following array a[ ] of integers: 530171092001237 Trace the function call n = process(a , 12 , 10 ) to determine the value of n 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)); } }

  10. n = process (a , 12 , 10 ); 05 3 0 17 10 9 20 0 1 2 3 710 1+ n=12, x=10 A[10] = 0 n = 4 1+ 1+ X=9 A[9] = 0 + X=11 A[11] = 0 0 0 0 + 1+ + X=8 A[8] = 0 X=10 X=10 X=12 0 0 X=7 X=9

  11. 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.

  12. long int z = Func( 5 , 3 ); z=10 n=5, m=3 6 4 n=4, m=3 + n=4, m=2 3 3 3 + 1 + n=3, m=3 n=3, m=2 n=3, m=2 n=3, m=1 1 1 1 2 2 + + 2 + 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

  13. 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:  2418482

  14. sum (Divide & Conquer) 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, m+1, e ); } }

  15. sum (Divide & Conquer) 29 s=0, e=6 M=3 14 15 s=0, e=3 M=1 + s=4, e=6 M=5 9 12 A[6] + 6 + s=0, e=1 M=0 s=2, e=3 M=2 s=4, e=5 M=4 s=6, e=6 A[0] A[4] A[1] A[2] A[3] + + + A[5] s=0,e=0 s=1,e=1 s=2,e=2 s=3,e=3 s=4,e=4 s=5,e=5

  16. 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.

  17. asciisum (Blanks included) int asciisum (string s, int first, int last) { if( first > last) return 0; return int ( s [first] ) + asciisum (s, first + 1, last); }

  18. asciisum (Blanksexcluded) 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); }

  19. 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.

  20. oddcount int oddcount(int A[], int s, int e) { if (s > e) return 0; else if ( A[s]%2 ) return 1 + oddcount (A, s+1, e); else return oddcount (A, s+1, e); }

  21. 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.

  22. Linear Search 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,s+1,e); } Call this function as: LinSearch(A, x, 0, n-1);

More Related