This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The problem may ask something like what this code will output if you call doSomething(5).
I'm learning recursion right now, and I find it really hard tracing the execution of these questions. Are there any techniques on doing these questions? Because I'm looking at some of these questions on previous AP exams and it takes me around 5 mins just to do one and I sometimes get lost on which recursive call I'm at. Especially the ones that made more than recursive call, they are really confusing. I usually don't have this much trouble with programming...
Well, for this particular problem, you might try looking at it from the other direction. That is, you can see that it starts with doSomething(5) , then calls doSomething(4), then doSometing(3) etc down to doSomething(0). Try looking at those in the reverse order.
What does doSomething(0) do?
What does doSomething(1) do?
What does doSomething(2) do?
And so on.
A pattern should start to emerge. Such patterns can be much easier to understand if you start with small parts of the pattern, before trying to understand the whole thing.
salvin francis wrote:You have probably printed out the execution trace of a different program(than the OP) here.
Well, yes, a factorial. Otherwise I'd be giving away too much specific info.
lso I do feel that it will be difficult to show in text the execution of [...]
I agree it's more complicated, but for people that are more visual, it can be really helpful. It took me these kinds of traces, along with some boxes to indicate nesting, to understand how recursion works. Not everybody might need such a tool, or it may be more confusing than helpful for some.To me, that's pretty noisy, but if I read it line-by-line, and I include a reference tree of the graph, I'm usually all set.
(And you'll notice my implementation is a little "backwards"