Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Recursive tracing java

  1. Apr 4, 2017 #1
    It would be much appreciated if you could show me how to do recursive tracing for this method.
    upload_2017-4-4_22-1-2.png
     
    Last edited: Apr 4, 2017
  2. jcsd
  3. Apr 4, 2017 #2

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    What's recursive in this method ?
    Did you try typing it in and see what it does ?
    Is this homework ?
     
  4. Apr 4, 2017 #3
    1) mystery(n-1) is recursive
    2) No it isn't homework. I'm studying for my exam
     
  5. Apr 4, 2017 #4

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    I see. Maybe I misunderstand 'recursive' : I then expect a call to 'mystery' inside the method. Don't see one, so your idea of recursive is something else. Can you explain ?
    Must be blind. Sorry.
     
  6. Apr 4, 2017 #5

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    All I've left is question 2 :smile:
     
  7. Apr 4, 2017 #6
    Yes I've tried to trace it and all I got was (6 + ((4 + ((2 + . I don't understand how to continue it.
     
  8. Apr 4, 2017 #7

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    I get
    Code (Text):

     
     ----- 6 ----------
    (6 + ((4 + ((2 + ( 0
     + 1)
    )
     + 3)
    )
     + 5)
    )
     --------------------------

     
    when I input the number 6 in :

    Code (Visual Basic):

    Sub mist(n As Integer)

        If n / 2 = 0 Then
       
            Debug.Print n
        ElseIf n Mod 2 = 0 Then
            Debug.Print "(" & n & " + ";
            mist n - 1
            Debug.Print ")"
        Else
            Debug.Print "(";
            mist n - 1
            Debug.Print " + " & n & ")"
        End If
       
    End Sub

    Sub test()
    Dim n As Integer
    again:
        n = CInt(InputBox("Give n "))
        Debug.Print "  "
        Debug.Print " ----- " & n & " ---------- "
        If n < 0 Then Exit Sub
        Call mist(n)
        Debug.Print " -------------------------- "
        GoTo again
    End Sub

     
     
  9. Apr 4, 2017 #8

    Mark44

    Staff: Mentor

    No. The mystery() function is recursive because it calls itself.

    BvU didn't ask about tracing the function; he asked if you had tried typing it in and running it. Seeing what the function produces might be helpful in understanding what it does.
     
  10. Apr 4, 2017 #9

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    I am not fluent in Java. I find 1 / 2 is integer division and yields zero, so in the context of Myst "if N/2==0" is equivalent to " if n= 1" in VB.
    If I take that into account the 0 goes away.
    (and if I add some ; at the end of the print statements it all ends up on one line:
    Code (Text):
     ----- 6 ----------
    (6 + ((4 + ((2 +  1 ) + 3)) + 5))
     --------------------------
    Wonder why your routine doesn't render the odd numbers ...
     
  11. Apr 4, 2017 #10

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Excel VBA is easy with its debugger at hand.
    Don't know what Java has for you. If no debugger, then perhaps some more print statements ?
     
  12. Apr 4, 2017 #11
    There's a systematic way to find out the tracing for any recursive method like trees method or in a table or whatever. I want to know the method you used to find out the tracing not the final answer. My exam is in paper so I won't have jcreator or VBA during the exam xD
     
  13. Apr 4, 2017 #12

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Didn't know that; was just trying to help. My systematic method has always been trial and error :rolleyes: -- especially the latter.
    Good luck with your exam !
     
  14. Apr 4, 2017 #13
    Thank you :).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Recursive tracing java
  1. About Recursion (Replies: 8)

  2. Sudoku Solver Problem (Replies: 12)

Loading...