![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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)
{
int reverseNum = 0;
if(num > 0)
{
reverseNum = reverseNum *10 + num % 10;
return reverseDigit(num/10);
}
else
return reverseNum;
}2nd Recursion method public static int reverseDigit(int num)
{
int reverseNum = 0;
if(num < 0)
{
return reverseNum;
}
else{
reverseNum = reverseNum *10 + num % 10;
return reverseDigit(num/10);
}
}Thank you for your help in advance.
__________________
-- pr0gm3r |
|
|
|
|
|
#2 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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
__________________
-- pr0gm3r Last edited by pr0gm3r; Oct 11th, 2005 at 10:01 AM. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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)
{
int newNumber = num / 10;
int newMsd = num % 10;
msd = msd * 10 + newMsd;
if (newNumber)
{
msd = reverseDigits (newNumber, msd);
}
return msd;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|