Write a Recursive Function Algorithm to find the terms of following recurrence relation.
t(1)=3
t(k)=2×t(k-1)-5 (n>1).
and (ii) If you call z←t(4) in a program then what value the
program will use for z?
Algorithm:
Algorithm Recursive(n):
if n = 1
then return 3
else x = Recursive(n - 1)
x = (2 * x) - 5
return x
endif
Trace of algorithm for t(4)
Manual trace of t(4):
Below is the C language code of this algorithm:
#include <stdio.h>
int Recursive(int n){
int x;
if(n == 1){
return 3;
}
else{
x = Recursive(n -1);
x = (2*x) - 5;
}
return x;
}
int main()
{
int z = Recursive(4);
printf("ans: %d \n", z);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.