DoubleOseven DoubleOseven - 1 month ago 5
C++ Question

Recursive c++ function

I am trying to create a recursive function as follows.

The function takes a counter

k
and as long that the counter is larger than zero I would like to call it recursively so that in the end I end up with something like this:

result = 2(2(2n+1)+1)+1


where the last n (when k=0) should be zero.

int pass(int k, int n)
{
if(k==0)
{
n = 0;
}
else
{
k--;
return pass(k, 2*n+1);
}
}


Can someone give me a hint as on how to do it?

Answer

Change

n = 0;

To

return n;

To return the result.

The rest of the code is fine

Comments