Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 11th, 2005, 4:21 AM   #1
pr0gm3r
Hobbyist Programmer
 
Join Date: Dec 2004
Location: CA
Posts: 102
Rep Power: 4 pr0gm3r is on a distinguished road
Send a message via MSN to pr0gm3r
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
pr0gm3r is offline   Reply With Quote
Old Oct 11th, 2005, 5:02 AM   #2
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by pr0gm3r
I have two different recursion method but both of them are not returning the rest of the value.
Please expand on this. What values are you giving them, and what are they returning?
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Oct 11th, 2005, 9:51 AM   #3
pr0gm3r
Hobbyist Programmer
 
Join Date: Dec 2004
Location: CA
Posts: 102
Rep Power: 4 pr0gm3r is on a distinguished road
Send a message via MSN to pr0gm3r
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.
pr0gm3r is offline   Reply With Quote
Old Oct 11th, 2005, 12:33 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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;
}
Originally called thusly: number = reverseDigits (number, 0);
__________________
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:47 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC