Sep 30, 2016 at 3:03am UTC
What is the best approach to make a mutually recursive function iterative?
Sep 30, 2016 at 3:04am UTC
Can you give us some pseudo-code?
Oct 3, 2016 at 1:30am UTC
Yes. I need to make it iterative. Would using goto be an approach? What is the best approach? I can't make them tail recursive (which is essentially iterative on most compilers) so that is not an option.
Oct 3, 2016 at 2:42am UTC
Can you write some your own pseudo-code to express your idea so that we can translate it to C++ language the way you want?
Oct 3, 2016 at 6:06am UTC
Here is the most stupid and trivial example I can think of:
1 2 3 4 5 6 7 8 9
bool is_even(unsigned x) {
if (x == 0) return true ;
else return is_odd(x - 1);
}
bool is_odd(unsigned x) {
if (x == 0) return false ;
else return is_even(x - 1);
}
Here's one way to make it iterative.
1 2 3 4 5
bool is_even(unsigned x) {
bool result = false ;
while (x-- != 0) result = !result;
return result;
}
You need to show some code. The form of the iterative solution depends on the nature of the problem you're solving.
Last edited on Oct 3, 2016 at 6:07am UTC