Make your own free website on Tripod.com

Topic 14 - Sample Questions - Recursion (5 days)

 

1.                            |     0   if a < b

       Q(a,b)   =        |      Q(a-b,b)      if b <= a

 

  a. find  Q(2,3)

  b. find Q(14,3)

  c. find Q(5861,7)

 

2.     L(n)   =      |    0                        if (n == 1)

                          |     L(n/2) + 1         if (n > 1)  

 

 a. find L(25)

 b. find L(100)

 c. find L(5000) 

 

3.                         |      5                                       if  j < k

       Q(j,k)   =      |      Q(j-k,k+2) + j                   if j>= k

 

 a. find Q(2,7)

 b. find Q(5,3)

 c. find Q(15,2)

 

4.                          |         g(b,a)                    if a < b

            g(a,b) =     |        a                            if b = 0

                             |         g(b,a % b)            otherwise

 

 a. find g(6,15)

 b. find g(20,28)

 c. find g(540,168)

 

5.                         |             3 * n                         if n < 5

            h(n) =       |         2 * h (n-5) + 7            otherwise

                               

 a. find h(2)

 b. find h(8)

 c. find h(24)

 

6.  int what(int n)

       { if (n > 0)

            return n + what(n-2);

          return 0;

      }

   a. n = what(7)

   b. n = what(14);

 

7, int who(int a,int b)

      { if (a==b)

          return a;

         else if (a > b)

              return who(a-1,b);

            else return who(a,b-1);

       }

 a. n = who(12,5);

 b. n = who(47,86);

 

8. int what2(int a,int b)

      {if (b==1)

          return a;

       else

           return b + what2(a-1,b+1);

     }

 a. n = what2(6,4);

 b. n = what2(3,12);

 

9. void print(int a,int b)

       {if (a==0 || b==0)

            return;

        System.out.println( a + b);

        print(a-1,b-2);

     }

a. print(6,12);

b. print(9,5);

 

10. void print(int n)

       { cout <<"Joe";

          if (n > 0)

           print(n-1);

       System.out.println("tom");

    }

a. print(7);

 

11. void print(apstring n, int a)

        {if (a < n.length())

            {System.out.println(n[a]);

              print(n,a+1);

            }

       }

a. print("susan",0);

b. print("danielle",3);

 

12. int who(int a,int b)

       {if (a ==1)

          return 8;

       else if (b==1)

           return 5;

        else return a + b + who(a-1,b-1);

      }

a. n = who(12,5);

b. n = who(8,3);

 

13. int what(int n)

      { if (n < 10)

           return n;

        else

            return n/2 + what(n/2)

      }

 a. n = what(12);

 b. n = what(145);

 

14. int getsum(int a, int b)  // precondition  a <= b 

 

15. int getsum(int a, int b)   // no precondition

 

16. int sum(int n)  // returns sum between 1 and n

 

17. int factorial(int n)  // returns factorial ex. 5 factorial is 120 because 5*4*3*2*1 = 120

 

18.void printcharacters(String n)  // prints characters each on new line

 

19. print reverse(String na)  // prints it in reverse

 

20. bool isthere(int ar[], int n)  // is number in vector

 

21. int smallest(int ar[] )   // returns smallest # in vector

 

22. void printdigits(int n)  // prints digits one at a time

 

23. int sumofdigits(int n)  // returns sum of digits

 

24. int sum(int ar[])  // returns sum of numbers in vector

 

 

 


HOME

1