I am trying to figure out this problem
consider this recurrence relation
f(1)=2
f(2)=2
f(n)=2*f(n-1)+f(-2) for n>2
write a recursive function to compute f.
I have some code that I have been working on. My question is.
How do u get the function to return a value that it computed back to itself to be used again .
When i run this for f(2) and f(1)it comes out right, but for anything else its wrong ( or at least i think.)
can anyone tell mat f(3) and f(4) are so i know what to look for? Also, give me a hint as to what I am doing wrong in the code?
Code:#include<iostream> using namespace std; int computef(int n) { int answer; if ((n=2)||(n=1)) return 2; else answer= 2*computef(n-1)+computef(n-2); cout << answer; } int main() { int x; cout << "Please input the number for calculation " ; cin >> x; cout << computef(x); system("pause"); return 0; }



LinkBack URL
About LinkBacks


