Page 443 - Computer science 868 Class 12
P. 443

4.   Differentiate between finite recursion and infinite recursion.                          [ISC 2022]
                   Ans.               Finite Recursion                            Infinite Recursion
                       Finite Recursion occurs when the recursion terminates  Infinite Recursion occurs when the recursion does not
                       after a finite number of recursive calls.    terminate after a finite number of recursive calls.
                       A recursion terminates only when a base condition is  As the base condition is never met, the recursion carries
                       met.                                         on infinitely.

                    5.  Design a class BinSearch to search for a particular value in an array. Some of the members of the class are given below:
                                                                                                                  [ISC 2020]
                        Data Members/Instance variables
                        arr[]                           :   to store integer elements
                        n                               :   integer to store the size of the array
                        Member Functions/Methods
                        BinSearch(int nn)               :   Constructor to initialise n = nn and declare the array
                        void fillarray( )               :   Accept ‘n’ numbers in array arr[]
                        void sort( )                    :   Sort the array in ascending order
                        int bin_search(int u, int l, int v)   :    Searches for the  value  ‘v’  using  binary  search and  recursive technique  and
                                                           returns its location if found otherwise returns -1
                        Define the class BinSearch giving details of the constructor( ), void fillarray( ), void sort( ) and int bin_search (int,int,int).
                       Define the main( ) function to create an object and call the functions accordingly to enable the task.

                   Ans.  import java.util.*;
                       class BinSearch
                       {
                        int arr[];
                        int n;
                        Scanner sc=new Scanner(System.in);
                        BinSearch(int nn) // constructor
                        {
                         n=nn;
                        }
                        void fillarray()
                        {
                          arr=new int[n];
                          System.out.println("Enter "+n + " elements");
                          for(int i =0;i<n;i++) // accepting numbers
                          arr[i]=sc.nextInt();
                        }
                        void sort()
                        { // Sorting in ascending order using bubble sort
                          int t;
                          for(int i=0;i<n-1;i++) {

                          for(int j =0;j<n-1-i;j++)
                          {
                           if (arr[j]>arr[j+1])
                           {
                             t=arr[j];
                             arr[j]=arr[j+1];
                             arr[j+1]=t;
                           }
                          }
                         }
                          System.out.println("Sorted array");


                                                                                                                       441
                                                                                                           Recursion   441
   438   439   440   441   442   443   444   445   446   447   448