• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help to debug Recursive

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have done some recursive problem like Fibonacci and factorial. I am trying to understand the following code which I created to understand recursive more. When I do debug them, I get lost. I need little explanation. If some can put some effort, I will be grateful


Here is my understanding:
#1 (arr, 0, 3)

       #2 (arr, 1, 3)
       #3 (arr, 2, 3)
       #4 (arr, 3, 3): test = 1 and #4 erased

#3 will call the method again and create stack #4 (arr, 3, 3) in the for loop. which will make test = 2 . #4 erased and # will increase test = 3
#2 will call the method again and create stack #4 (arr, 2, 3) and #5 (arr, 3,3) in the for loop. #5 makes test = 3 and # 4 make test = 4. Stack #3,4 erased. #2 makes test = 5
#1 will increase test = 7

This understanding can be wrong. I am looking for some help please


 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Debugging recursive methods can be confusing, try adding some debug so you can get a print out of what is happening. For example:



Note: I renamed your variable 'l' to 'j' as it can be hard to distinguish an 'l' from a '1'
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the object is to write a recursive method that returns the total of the array, your method can be a lot simpler.

* Are you using int total at all?
* Since you pass the array, do you need to pass the array length?
* You don't need a class variable like static int test.
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no reason to use array here for sure. All I ask to help me  to understand the stack and method call in the for loop here.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Similar to Tony's example but I like to add a "level" parameter.

Output
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic