1

I was trying a random program to practice recursion.

I am unable to figure out what is happening

int fun(int *list,int l,int h) { if(l==h) return list[l]; int b = fun(list,l+1,h)+ fun(list,l,h-1); printf("%d \n",b); return b; } void main() { int ar[]= { 1,2,3,4}; fun(ar,0,3); } 

Could someone draw recursion tree maybe? Also what is the value of b at each point? Thanks!

14
  • 1
    try running the program and using your debugger. Commented May 26, 2014 at 8:04
  • 4
    Note that values of b will be undefined since fun doesn't actually return anything. Commented May 26, 2014 at 8:05
  • 1
    Did you get no warning from your compiler that fun doesn't return an int? Commented May 26, 2014 at 8:06
  • 1
    @user3308043 Why does that sound more complicated? I seem to have extra boxes now. What do I do with them? WHAT? Commented May 26, 2014 at 8:09
  • 1
    @user3308043 I know what you are doing, I just found it a bit over-complicated, but to each his own. One thing though, it is true a the stack can overflow from too much recursion. However, recursion is not the only way a stack can overflow. One way to improve recursion is to do tail recursion (en.wikipedia.org/wiki/Tail_call) Commented May 26, 2014 at 8:20

3 Answers 3

4

A slight retool of your app will likely tell you exactly what you want to see. Modifying your algorithm to include a depth-parameter and some creative lead-space printing with an argument-specified left-justificaction gives us this:

#include <stdio.h> #include <stdlib.h> int fun(int *list, int l, int h, int d) { printf("%*sfunc(list, %d, %d)\n", d, "", l, h); int b = (l==h) ? list[l] : fun(list, l+1, h, d+2)+ fun(list, l, h-1, d+2); printf("%*sresult: %d \n", d, "", b); return b; } int main() { int ar[]= { 1,2,3,4}; fun(ar, 0, 3, 0); return EXIT_SUCCESS; } 

The results somewhat speak for themselves:

Output

func(list, 0, 3) func(list, 1, 3) func(list, 2, 3) func(list, 3, 3) result: 4 func(list, 2, 2) result: 3 result: 7 func(list, 1, 2) func(list, 2, 2) result: 3 func(list, 1, 1) result: 2 result: 5 result: 12 func(list, 0, 2) func(list, 1, 2) func(list, 2, 2) result: 3 func(list, 1, 1) result: 2 result: 5 func(list, 0, 1) func(list, 1, 1) result: 2 func(list, 0, 0) result: 1 result: 3 result: 8 result: 20 

With a few modifications (not shown here; left as an exercise for the reader), you can draw the terminal-dependent line art chars to amplify even more the "what called what and returned what?" question. The following was the output of such a mod using VT100 compliant escape sequences (which SO thankfully consumes nicely in their formatting). Enjoy!

┌func(list, 0, 3) │ ┌func(list, 1, 3) │ │ ┌func(list, 2, 3) │ │ │ ┌func(list, 3, 3) │ │ │ └result: 4 │ │ │ ┌func(list, 2, 2) │ │ │ └result: 3 │ │ └result: 7 │ │ ┌func(list, 1, 2) │ │ │ ┌func(list, 2, 2) │ │ │ └result: 3 │ │ │ ┌func(list, 1, 1) │ │ │ └result: 2 │ │ └result: 5 │ └result: 12 │ ┌func(list, 0, 2) │ │ ┌func(list, 1, 2) │ │ │ ┌func(list, 2, 2) │ │ │ └result: 3 │ │ │ ┌func(list, 1, 1) │ │ │ └result: 2 │ │ └result: 5 │ │ ┌func(list, 0, 1) │ │ │ ┌func(list, 1, 1) │ │ │ └result: 2 │ │ │ ┌func(list, 0, 0) │ │ │ └result: 1 │ │ └result: 3 │ └result: 8 └result: 20 
Sign up to request clarification or add additional context in comments.

2 Comments

Hadn't seen this clever usage of "%*s". Nice :)
@RSahu I've done it so many times in recursive algorithms I actually start writing them with this already in place just to sanity-check the call stack. Btw, if single threaded you can avoid the parameter and use a local-static, doing the same thing (see it live). I just chose not to for this example. Glad you liked it.
3

I guess it should be something like this (not very tree-ish though, just didn't know how else to draw it):

fun(ar, 0, 3) -> { (prints 20) fun(ar, 1, 3) -> { (prints 12) fun(ar, 2, 3) -> { (prints 7) fun(ar, 3, 3) + fun(ar, 2, 2) } + fun(ar, 1, 2) -> { (prints 5) fun(ar, 2, 2) + fun(ar, 1, 1) } + fun(ar, 0, 2) -> { (prints 8) fun(ar, 1, 2) -> { (prints 5) fun(ar, 2, 2) + fun(ar, 1, 1) } + fun(ar, 0, 1) -> { (prints 3) fun(ar, 1, 1) + fun(ar, 0, 0) } } } 

2 Comments

could you also emit the output at each point? value of b maybe?
Actually I can't :-). See the comment of simonc above. However I suppose I could do it assuming you return b in your function.
3
1 fun(list,0,3) 2 = fun(list,1,3) + fun(list,0,2) 3 = fun(list,2,3) + fun(list,1,2) + fun(list,1,2) + fun(list,0,1) 4 = fun(list,2,2) + fun(list,3,3) + fun(list,1,1) + fun(list,2,2) + fun(list,1,1) + fun(list,2,2) + fun(list,0,0) + fun(list,1,1) 

4th line does not have any printf statements.

3rd line will print

7 5 5 3 

2nd line will print

12 8 

1st line will print

20 

2 Comments

could you specify the line numbers explicitly in the answer?
Line numbers added. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.