This is just a piece of code I've found over the Internet, and I'm trying to understand why the output is 3 1 1 2 1 1 2 3. Could anyone explain using a stack representation or something else, I am really stuck on this one.

#include <iostream>
using namespace std;

void ex233(int n)
{
    if (n <= 0) return;
    cout << n << endl;
    ex233(n-2);
    ex233(n-1);
    cout << n << endl;
}

int main()
{
    ex233(3);

    return 0;
}

I did read about recursion but I didn't find any good examples on how recursion works when the function is called more than once, in my case ex233(n-2) followed by ex233(n-1).
Thanks.

Recommended Answers

All 4 Replies

ex233(0 or less) does nothing.

ex233(1) outputs 1, calls ex233(-1), calls ex233(0), outputs 1. We already know about ex233(-1) and ex233(0), so this can be summarised as outputs 1, outputs 1

ex233(2) outputs 2, calls ex233(0), calls ex233(1), outputs 2.

ex233(3) outputs 3, calls ex233(1), calls ex233(2), outputs 3.

Now you fill in that last one a bit more; what does ex233(1) do and what does ex233(2) do? When you've worked those out, you can replace them in ex233(3) and you're done. You have everything you need right here in this post.

ex233(2) outputs 2, calls ex233(0), calls ex233(1), outputs 2.
Umm why does ex233(1) outputs 2?
ex233(2) seems to print 2 1 1 2

Here's how I'm trying it on paper:
(1) ex233(3) outputs 3, then calls ex233(1)
(2) ex233(1) outputs 1, then calls ex233(-1)
STUCK NOW! How does it continue from now on? Really confused .. the output seems to be large but I see the program stops right after ex233(1)...

Umm why does ex233(1) outputs 2?

Read what I said again. In fact, I'll break it down for you.
I said this:

ex233(2) outputs 2, calls ex233(0), calls ex233(1), outputs 2.

So let's break it down:
ex233(2):

1) outputs 2.

2) calls ex233(0). That will do something.

3) calls ex233(1). That will do something.

4) Outputs 2.

I did not say that ex233(1) outputs 2.

(1) ex233(3) outputs 3, then calls ex233(1)
(2) ex233(1) outputs 1, then calls ex233(-1)
STUCK NOW! How does it continue from now on?

What happens when a function ends? It returns back to where it came from, and execution carries on from there.

(1) ex233(3) outputs 3, then calls ex233(1)
(2) ex233(1) outputs 1, then calls ex233(-1)

ex233(-1) returns, so we're back in ex233(1). What does ex233(1) do next, AFTER calling ex233(-1) ?

My post above contains everythig you need. You just have to replace each ex233(x) with what that function does.

ex233(3):
1) outputs 3
2) calls ex233(1)
3) calls ex233(2)
4) outputs 3.

Let's replaces the ex233(1)

ex233(3):
1) outputs 3
2a) outputs 1
2b) calls ex233(-1)
2c) calls ex233(0)
2d) outputs 1.
3) calls ex233(2)
4) outputs 3.

Let's replace the ex233(-1)

ex233(3):
1) outputs 3
2a) outputs 1
2b) does nothing
2c) calls ex233(0)
2d) outputs 1.
3) calls ex233(2)
4) outputs 3.

Can you do the rest?

Got it now. Extremely helpful, thanks very much:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.