Dynamic programming solutin


hello everyone,,,

i have a question regarding a part of my code,,,,i don't understand y I don't get the correct answer in my output,,please help me :)

I am applying the dynamic prog. sol'n to the following function:

f(n)=f(n-1)-f(n-2)+f(n-3) , n>4

f(n)=n+2 , n<=4

Here is the section that gives me the wrong output:

int dynamic_f(int n)
{
if(n<=4)
return (n+2);

int a=2;
int b=3;
int c=4;
int d;

for(int i=5;i<=n;i++)
{

d=c-b+a;

a=b;
b=c;
c=d;

}
return d;
}


NB: I don't get any errors or warnings when compiling but the output is wrong for example if i run the code for n=6 i should get d=4 but instead i get d=2




}
Last edited on
Topic archived. No new replies allowed.