I have the following function:
int v[]={0,1,1,1};
int f(int x)
{
if(x>=1) return v[x]+f(x-1);
else return 0;
}
When I called it in main like so: cout<<f(4); It’s outputting ‘4’ but I expect to be ‘3’
Can someone clarify why I get ‘4’ and not ‘3’ ? (I think I’m missing something)
v[4] is not defined. It has 4 elements, so the max array index is 3.