GATE / CS ARCHIVE

GATE CSE 2008 / QUESTION 75

Values of matching recurrence implementations

Programming in C · Recursion · Full GATE CSE 2008 paper

GATE 2008, Q75 2 marks · MCQ
Report issue

Recursion

Question 75, GATE 2008: Consider these C functions, assuming valid array bounds and the intended unit-cost arithmetic model:
int f1(int n) {
 if(n==0 || n==1) return n;
 else return 2*f1(n-1)+3*f1(n-2);
}
int f2(int n) {
 int i; int X[N],Y[N],Z[N];
 X[0]=Y[0]=Z[0]=0;
 X[1]=1; Y[1]=2; Z[1]=3;
 for(i=2;i<=n;i++) {
  X[i]=Y[i-1]+Z[i-2];
  Y[i]=2*X[i]; Z[i]=3*X[i];
 }
 return X[n];
}
What values do f1(8) and f2(8) return?

(A) 1661 and 1640
(B) 59 and 59
(C) 1640 and 1640
(D) 1640 and 1661
Question 75, GATE 2008: Consider these C functions, assuming valid array bounds and the intended unit-cost arithmetic model:
int f1(int n) {
 if(n==0 || n==1) return n;
 else return 2*f1(n-1)+3*f1(n-2);
}
int f2(int n) {
 int i; int X[N],Y[N],Z[N];
 X[0]=Y[0]=Z[0]=0;
 X[1]=1; Y[1]=2; Z[1]=3;
 for(i=2;i<=n;i++) {
  X[i]=Y[i-1]+Z[i-2];
  Y[i]=2*X[i]; Z[i]=3*X[i];
 }
 return X[n];
}
What values do f1(8) and f2(8) return?

(A) 1661 and 1640
(B) 59 and 59
(C) 1640 and 1640
(D) 1640 and 1661
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. Both functions implement a0=0, a1=1, an=2a(n−1)+3a(n−2). In f2, Y[i]=2X[i] and Z[i]=3X[i], including the initialized indices, so X has exactly that recurrence.
2. Values a2 through a8 are 2,7,20,61,182,547,1640. Thus both return 1640, option C, assuming N≥9.
3. Equivalently an=(3^n−(−1)^n)/4.

Original paper, page 16