3
\$\begingroup\$

I have this code:

public class RecurLoopTest {
 public static void main(String[] args) {
 printit(2); 
 }
 private static int printit(int n){
 if(n>0){
 printit(--n);
 }
 System.out.print(n+",");
 return n;
 }
}

I have drawn a execution flow/memory flow diagram for above program. Is this diagram correct or do I need some changes to make it correct?

What I have drawn looks like this:

enter image description here

Link to edit diagram

Link to view diagram

hc_dev
8546 silver badges15 bronze badges
asked Apr 20, 2014 at 2:34
\$\endgroup\$
1
  • \$\begingroup\$ Wow, this diagram visually clearly communicates your recursive algorithm: illustrated call-stack plus count-down iterator-parameter👍 \$\endgroup\$ Commented May 8, 2021 at 12:29

1 Answer 1

2
\$\begingroup\$

It would be better to add the method arguments in the diagram, for example:

printit(0) -- n=0, prints "0,", returns 0
printit(1) -- n=1, prints "0,", returns 0
printit(2) -- n=2, prints "1,", returns 1
main()

I don't think you need 4 drawings side by side for this, just one like this would be clear enough already.

answered Apr 20, 2014 at 6:58
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.