Вы находитесь на странице: 1из 6

1 What’s recursion?

A recursive function is simply a function that repeatedly calls itself and the trick is to
realize when to stop calling ourselves to avoid infinite loops that result in stack overflows.

2 Write a function that returns all permutations of a given list.


1 permutations({1, 2, 3})

2 [1, 2, 3]

3 [1, 3, 2]

4 [2, 1, 3]

5 [2, 3, 1]

6 [3, 1, 2]

7 [3, 2, 1]

3 Why Stack Overflow error occurs in recursion?

Ans If the base case is not reached or not defined, then the stack overflow problem may arise.

Ans All subtrees are balanced

4 Given a linked list, write a function that prints the nodes of the list in reverse order.
1,2,3
Ans
printReversedList(1 -> 2 -> 3)

3
eg.
2

1
5 When should you use recursion?
We can use recursion to do anything that you can do with non-recursive code. There are a
lot of cases where writing a function recursively will be a lot more effort and may run less
efficiently than the comparable iterative code.
6 Is it easier to solve it recursively than iteratively?
Any problem can be solved either recursively or iteratively, so you just have to decide
which is easier.
7 What’s the base case?
The base case is when we reach an input for which we know what the output would be.”
The base case is thus the condition which terminates recursion.
In the example here, the base case is when the input has nothing for us to loop through. It
could be because the input is nil (or an empty hash). When that happens, we know the
output is {books: 0, movies: 0, articles: 0}.

8 Why is recursion not commonly used?


With iteration, there is only one function call and within that function call, we execute the
loop. With recursion, we make multiple function calls, breaking our problem into smaller
pieces until we can solve each one.
9 Question: How does the recursive algorithm works?
Answer: The recursive algorithm divides a problem into smaller, easy-to-manage sub-
problems. The output gained from one recursion after processing one sub-problem
becomes input for the subsequent recursive process.
10 Write a recursive function to determine prime number?

int isPrime(int x, int d) {


        if (d == 1)
                return 1;

        if (!(x % d))


                return 0;

        return isPrime(x, d-1);


}

main(int ac, char **av) {


        if (ac != 2)
                exit(1);

        printf("%d\n", isPrime(atoi(av[1]), (int) sqrt(atoi(av[1]))));


}

11 Predict the output of the questions –


Function main()
{
double d = 123.4
static float f =123.4
if (m equals i)
print ”Both of them are equal”
else if( f > d )
print ”Float is greater”
else
print ”Double is greater”
}
ANS Float and Double both are equal

12 Predict the output of the following program.

int fun1(int x, int y) 

{
  if(x == 0)
    return y;
  else
    return fun1(x - 1,  x + y);
}

Answer: The function fun() calculates and returns ((1 + 2 … + x-1 + x) +y) which is
x(x+1)/2 + y. For example if x is 5 and y is 2, then fun should return 15 + 2 = 17.

13 Predict the output of the following program.

void fun2(int arr[], int start_index, int end_index)

{
  if(start_index >= end_index)   
     return;
  int min_index; 
  int temp; 
  
  /* Assume that minIndex() returns index of minimum value in 
    array arr[start_index...end_index] */
  min_index = minIndex(arr, start_index, end_index);
  
  temp = arr[start_index];
  arr[start_index] = arr[min_index];
  arr[min_index] = temp;        
  
  fun2(arr, start_index + 1, end_index);
}     
Answer: The function fun2() is a recursive implementation of Selection Sort.

14 Predict the output of the following program.


#include <iostream>
using namespace std;
void fun(int n) 

    if(n > 0) 
    { 
        fun(n - 1); 
        cout << n <<" "; 
        fun(n - 1); 
    } 

  
int main() 

    fun(4); 
    return 0; 

121312141213121
fun(4)
/
fun(3), print(4), fun(3) [fun(3) prints 1 2 1 3 1 2 1]
/
fun(2), print(3), fun(2) [fun(2) prints 1 2 1]
/
fun(1), print(2), fun(1) [fun(1) prints 1]
/
fun(0), print(1), fun(0) [fun(0) does nothing]
15 Predict the output of following program.
#include <iostream>
using namespace std;
  
void fun(int x) 

    if(x > 0) 
    { 
        fun(--x); 
        cout << x <<" "; 
        fun(--x); 
    } 

  
int main() 

    int a = 4; 
    fun(a); 
    return 0; 

Output:
0120301
16 Predict the output of following program. What does the following fun() do in general?
#include <iostream>
using namespace std;
   
int fun(int a[],int n)
{
  int x;
  if(n == 1)
    return a[0];
  else
    x = fun(a, n - 1);
  if(x > a[n - 1])
    return x;
  else
    return a[n - 1];
}
  
int main()
{
  int arr[] = {12, 10, 30, 50, 100};
  cout << " " << fun(arr, 5) <<" ";
  getchar();
  return 0;
}

Output: 100
fun() returns the maximum value in the input array a[] of size n.

17 What does the following function do for a given Linked List?


void fun1(struct Node* head)
{
if(head == NULL)
    return;
  
fun1(head->next);
cout << head->data << " ";
}
Ans fun1() prints the given Linked List in reverse manner. For Linked List 1->2->3->4->5,
fun1() prints 5->4->3->2->1.
18 Explain the functionality of following functions.

int fun1(int n) 



if(n == 1) 
    return 0; 
else
    return 1 + fun1(n/2); 

Answer: The function calculates and returns  . For example, if n is between 8 and
15 then fun1() returns 3. If n is between 16 to 31 then fun1() returns 4.

19 Predict the output of following program. What does the following fun() do in general?

Output: 91
fun(99) = fun(fun(110)) since 99 ? 100
= fun(100) since 110 > 100
= fun(fun(111)) since 100 ? 100
= fun(101) since 111 > 100
= 91 since 101 > 100
Returned value of fun() is 91 for all integer rguments n 101. This function is known
as McCarthy 91 function.

20 What is base condition in recursion?

In the recursive program, the solution to the base case is provided and the solution of the
bigger problem is expressed in terms of smaller problems.

Оценить