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.