Hactar Posted March 24, 2005 Posted March 24, 2005 I'm learning recursive functions in my AP computer science class (which is taught in JAVA) and my teacher gave this question to us as an extra. public static void Mystery (int n) { if (n < 0) return n; System.out.println(n); Mystery(n-1); System.out.println(n); } What I understand from it is that it takes the number (n) and reduces it until it gets to zero. It prints it before it subtracts 1 from it. So it would look like this (if I entered 4): 4 3 2 1 0 But, he said that that final product should look like this: 4321001234 What I don't understand is how it adds to 'n': I see that it prints 'n', then reduces it by 1, then prints it again. Maybe because its hard for me to manipulate numbers in my head that I'm not understanding it, but for the life of me I can't see where it is adding to 'n'. Also, there isn't any additional code to this: he just wrote it on the board as a stand-alone problem. Before I could really discuss it with him the class ended and I had to leave. What do you guys think?
Cadmus Posted March 24, 2005 Posted March 24, 2005 First of all, note that your output is all on a single line. However, what is used is not print(), but println(), which causes line feeds. You meant to use print(). For your personal information, note also that it is typically not considered good form to begin method names with an uppercase letter. Consider a simpler case, where n=1. The if statement fails, and the print prints 1. Then, Mystery(0) is called. The if statement fails, and the print prints 0. Then, Mystery(-1) is called. The if statement succeeds, and the method returns. The calling method then contintues, after the call to Mystery(-1), printing out n, which was 0. The method ends, and it returns to the calling method. The calling method then continues, after the call to Mystery(0), printing out n, which was 1. The initial method call then returns. Examining the output, we have 1001. If we extrapolate to an initial n=4, and change the println() to pint(), then I would anticipate the output that your anticipated. I think that if you run this problem, you will get this output. I think that your problem is that you do not realize that there are 2 print() statements for each value of n. Therefore, each value will be printed twice. In between the calls to print(), there are calls to Mystery() with a lower number, and so its pair of print() will appear between the print() for the earlier call with the higher number. The final call will be with n=0, and therefore 2 zeros will be at the center.
Hactar Posted March 25, 2005 Author Posted March 25, 2005 I read over your post a few times, and I think I understand it now. For some reason I have a really hard time understanding things like programming and so its a difficult subject for me. About starting mystery() with an uppercase letter, I never knew that was bad form. My teacher always does that and hasn't said anything about it. Thanks alot for your help!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now