Page 423 - Computer science 868 Class 12
P. 423

if (a.…………………)
                                  return b.…………………
                           else
                           {  dig=c.…………………
                                 d.…………………
                         }
                         }
                    2.  Fill in the blanks to return product of all numbers in an array using recursive method
                        int prod(int arr[],int i) where arr[]={15,3,21,7,18} and i=0
                        int prod(int arr[],int i)
                        { if(a.…………………)
                            b.…………………
                          else
                           c.…………………
                        }
                    3.  Fill in the blanks to count the frequency of ‘A’ in a String using recursive method int count(String s, int p)  where s = ”ABRACADABRA”
                       and p = 0
                        int count(String s,int p)
                        { if(a.…………………)
                                  b.…………………
                            else if c.…………………
                                  return 1+ d.…………………
                            else
                                  e.…………………
                        }

                   Answers
                     1.  a. n==0      b.  return 0;       c.  n%10;      d.  return dig+sumofdigit(n/10);
                      2.  a. i==arr.length     b.  return 1;         c.  return arr[i]*prod(arr,i+1);
                      3.  p==s.length()      b.  return 0;       c.  s.charAt(p)==’A’  d.  count(s,p+1);     e. return count(s,p+1);

                 C.  Answer the following questions:
                    1.  Write a few uses of recursion.
                   Ans.  Recursion is used in:
                       •  Back tracking algorithm
                       •  Tree traversal
                       • Tower of Hanoi
                    2.  Write two disadvantages of recursion.
                   Ans.  •  It consumes a lot of memory. As the method calls itself repeatedly, every time the variable is allocated with a new memory on
                        the stack.
                        • Its memory stack manipulation recursion is a slow process.
                    3.  Write a difference between recursion and iteration.
                   Ans.
                                         Recursion                                    Iteration
                       •  Fresh memory space is allocated for each recursive  •  It uses  same memory  locations for variables and
                          call.                                        repeats the same unit of code.
                       •  It is a slow process.                     •  It is faster than recursion.
                    4.  Predict the output of the following recursive code when n = 27. Also tell what this method is doing.
                        class Output1
                        {   void calling(int nn)
                            {
                                int f=2;
                                show(nn , f);
                            }
                         void show(int n, int f)

                                                                                                                       421
                                                                                                           Recursion   421
   418   419   420   421   422   423   424   425   426   427   428