Convert this to non-recursive
Code Java:
public static int f(int a, int b)
{
Z.h1(a, b); //// arbitrary code
if (Z.h2(a, b))
return Z.h3(a, b);
else
{
Z.h4(a, b); ////arbitrary code
return f(Z.h5(a, b), Z.h6(a, b)); //// recursive call
}
}
-h1 and h4 --------------------> void functions
-h2 ------------------------------> boolean function
-h3, h5 and h6 -------------->return int
-h1 ... h6-----------------------> will not call f
My Attempt to convert the code above to non-recursive:
Code Java:
public static int f(int a, int b)
{
while(true)
{
Z.h1(a, b); //// arbitrary code
if (Z.h2(a, b)) //// base case
return Z.h3(a, b);
else
{
Z.h4(a, b); /////arbitrary code
int temp = Z.h5(a, b);
b = Z.h6(a, b));
a=temp;
}
}
}
I'm not sure if I'm right.
Any ideas?
Re: Convert this to non-recursive
It looks fine to me. Did you test it out to make sure the outputs match up?