edited by
540 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

2 votes
2 votes
0 answers
1
2 votes
2 votes
1 answer
3
2 votes
2 votes
0 answers
4
srestha asked Jan 28, 2019
621 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)?