edited by
586 views
0 votes
0 votes

Consider the following function foo()

void foo(int n)
{
    if(n<=0) printf("Bye");
    else
    {
        printf("Hi");
        foo(n-3);
        printf("Hi");
        foo(n-1);
    }
}

Let P(n) represent the recurrence relation indicating the number of times the print statement (both “Hi” and “Bye” included) is executed. Then which of the following is the best match for P(n)?

  1. $\text{P(n)=P(n-1)+P(n-3)+2 for n>=0;1 for n=0}$
  2. $\text{P(n)=P(n-1)+P(n-3)+2 for n>0;2 for n=0}$
  3. $\text{P(n)=P(n-1)+P(n-3)+2 for n>0;0 for n=0}$
  4. $\text{P(n)=P(n-1)+P(n-3)+1 for n>0;2 for n=0}$

Ans. A

But when n=1, the o/p will be “Hi Bye Hi Bye” i.e 4 times printed.

From A, P(1)=P(0)+P(-2)+2 = 1+P(-2)+2=3+P(-2)

But nothing is mentioned about P value when n<0.

How to solve for P(-2) and other negative values?

 

edited by

Please log in or register to answer this question.

Related questions

431
views
0 answers
2 votes
Akash Mishra asked Jan 16, 2018
431 views
Consider the following C program:The output produced by above C program is ________. (Assume int is of 2 bytes)
621
views
0 answers
0 votes
Ram Swaroop asked Jan 29, 2019
621 views
Consider the following C code:#include <stdio.h>struct MadeEasy{char p,q,r;};int main (void){struct MadeEasy a={ d' - 2019,'e',5+'a'};struct MadeEasy *b=&a;printf("%c, %c...
1.5k
views
1 answers
2 votes
Ram Swaroop asked Jan 29, 2019
1,527 views
Consider the following C code:include <stdio.h>int fun(){static int num=25;return num ;}int main(){for(fun( ); fun();fun())printf("%d", fun( ));return O;}The sum of the v...
695
views
0 answers
2 votes
srestha asked Jan 28, 2019
695 views
void foo(int n) { for(i1=1;i1<=n;i1++) { for(i2=1;i2<=i1;i2++) { ....... { for(i6=1;i6<=i5;i6++) { count++; } } } } }Count initially 0.What is value returned by foo(8)?