- #1
Neon32
- 68
- 1
It would be much appreciated if you could show me how to do recursive tracing for this method.
Last edited:
BvU said:What's recursive in this method ?
Did you try typing it in and see what it does ?
Is this homework ?
I see. Maybe I misunderstand 'recursive' : I then expect a call to 'mystery' inside the method.Neon32 said:1) mystery(n-1) is recursive
BvU said:Did you try typing it in and see what it does ?
BvU said:All I've left is question 2
----- 6 ----------
(6 + ((4 + ((2 + ( 0
+ 1)
)
+ 3)
)
+ 5)
)
--------------------------
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
No. The mystery() function is recursive because it calls itself.Neon32 said:1) mystery(n-1) is recursive
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.Neon32 said:Yes I've tried to trace it and all I got was (6 + ((4 + ((2 + . I don't understand how to continue it.
----- 6 ----------
(6 + ((4 + ((2 + 1 ) + 3)) + 5))
--------------------------
Thank you :).BvU said:Didn't know that; was just trying to help. My systematic method has always been trial and error -- especially the latter.
Good luck with your exam !
Recursive tracing in Java refers to the process of tracing the execution of a recursive method, which is a method that calls itself in order to solve a problem. It involves keeping track of the values of variables and the order in which the method calls itself, in order to understand how the method is working and identify any errors or inefficiencies.
Recursive tracing is important in order to understand how a recursive method is functioning and to identify any potential issues or bugs. It also allows for a deeper understanding of the logic behind the method and can help with troubleshooting and optimizing the code.
To perform recursive tracing in Java, you will need to have a basic understanding of how recursive methods work and be familiar with concepts such as base cases and recursive calls. Then, you can use techniques such as printing out the values of variables and keeping track of the order of method calls to trace the execution of the recursive method.
One common mistake when performing recursive tracing in Java is not properly identifying and handling base cases, which can lead to an infinite loop. It is also important to keep track of the order of method calls to ensure that the recursive calls are being made in the correct order.
Recursive tracing can help with debugging and optimizing code by allowing you to see exactly how the recursive method is functioning and where any errors or inefficiencies may occur. By tracing the execution, you can identify and fix any issues, as well as potentially improve the efficiency of the code by finding ways to reduce the number of recursive calls or improve the logic of the method.