![]() |
Debug recursion method()
Hi guys,
I need your expertice to debug my recursion method... Here is the problem: I am trying to figure out how come my code is not working properly. The design and algorithm are correct but it seem that the recursion method did not fully return the whole value that passed. I have two different recursion method but both of them are not returning the rest of the value . below is the code: 1st recursion method :
public static int reverseDigit(int num)2nd Recursion method :
public static int reverseDigit(int num)Thank you for your help in advance. |
Quote:
|
my input is an integer such as: 78491
and it should return 19487 but the method just return: 0 btw this is my homework... hehehehe.. On the recursive method I can only pass one parameter and no global variable... maybe the reverseNum is always set to 0 because I delcared it at the first level of the recursive called... hehehe... very starnge to me... Thank you for your help and when I find the solutions I'll post it my answer again in this forums ^__^ GBU |
Firstly, you just have some serious logical errors. There's nothing there you can't find if you but invest in a pencil, some paper, and the time for a couple of iterations.
Now, as to your approach, in general. I don't know why you're using recursion, at it seems inefficient for this particular task, but let's say that's beside the point. You are determining the least significant digit, then spiralling into the recursion to do it again. When you're fully nested, your least significant digit is the most significant of the original number. When you back out, multiplying by 10, you are simply reconstructing the number in its original form. Without any real time spent in analysis of the BEST approach, I would say the solution, with the GIVEN approach, is to carry in the MSD, not the LSD, then reconstruct the number. This is C syntax, but I presume you can convert it easily enough. :
int reverseDigits (int num, int msd) |
| All times are GMT -5. The time now is 11:18 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC