Welcome to Dream.In.Code
Become a Java Expert!

Join 150,370 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,814 people online right now. Registration is fast and FREE... Join Now!




Need some help with recursive method

 
Reply to this topicStart new topic

Need some help with recursive method, "some" used quite conservatively...

tex1ntux
16 Feb, 2008 - 02:54 AM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2008
Posts: 14


My Contributions
This is an assignment, so don't help me too much.
We have to write a recursive method that accepts 1 int and outputs this when given '4':
QUOTE
Enter rabbit: n=4
Enter rabbit: n=3
Enter rabbit: n=2
Leave rabbit: n=2 value=1
Enter rabbit: n=1
Leave rabbit: n=1 value=1
Leave rabbit: n=3 value=2
Enter rabbit: n=2
Leave rabbit: n=2 value=1
Leave rabbit: n=4 value=3


Conceptually I think that it is supposed to:
(1.) Output all rabbits from itself to 1
(2.) Have the rabbit directly below the one calling it do (1.)
(3.) Display the recursive level of the rabbit in the tree when it leaves (finishes doing (2.))

I'd show you the code I've come up with, but it's just 15 pathetic lines that I've poured half a dozen hours into and don't even do (1.) anymore ever since I tried to make them do 2 & 3. Just know that I have put a bunch of thought into this problem, I'm probably over complicating it in my head, and I'm on narcotics (hydrocodone) because of 3 root canals I just got done and so my brain isn't cooperating with my will to solve this problem.

I think I can figure out (1.) and (2.), but (3.) seems more enigmatic. Maybe it will be easier to grasp once I have (1.) and (2.) working?
I guess my main question is whether or not you agree that a program that does (1.), (2.), and (3.) will satisfy the requirements, and whether or not you have any suggestions on improving (1.), (2.), and (3.) or implementing them.

Thank you.

This new version sort of does (1.) and (3.), but (2.) is still at large.
CODE
      public int recursive(int r){

        if( r == 0) { return 0; }
        else{
            System.out.println("Enter rabbit: n=" + r);
            recursive( r - 1);
        }
         System.out.println("Leave rabbit: n=" + r + " value=" + counter);
         counter++;
         return 0;
      }


This post has been edited by tex1ntux: 17 Feb, 2008 - 02:06 AM
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Need Some Help With Recursive Method
16 Feb, 2008 - 03:39 AM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
We need to see your code.
User is offlineProfile CardPM
+Quote Post

dato.java
RE: Need Some Help With Recursive Method
16 Feb, 2008 - 10:34 AM
Post #3

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 30

QUOTE(tex1ntux @ 16 Feb, 2008 - 03:54 AM) *

This is an assignment, so don't help me too much.
We have to write a recursive method that accepts 1 int and outputs this when given '4':
QUOTE
Enter rabbit: n=4
Enter rabbit: n=3
Enter rabbit: n=2
Leave rabbit: n=2 value=1
Enter rabbit: n=1
Leave rabbit: n=1 value=1
Leave rabbit: n=3 value=2
Enter rabbit: n=2
Leave rabbit: n=2 value=1
Leave rabbit: n=4 value=3


Conceptually I think that it is supposed to:
(1.) Output all rabbits from itself to 1
(2.) Have the rabbit directly below the one calling it do (1.)
(3.) Display the recursive level of the rabbit in the tree when it leaves (finishes doing (2.))

I'd show you the code I've come up with, but it's just 15 pathetic lines that I've poured half a dozen hours into and don't even do (1.) anymore ever since I tried to make them do 2 & 3. Just know that I have put a bunch of thought into this problem, I'm probably over complicating it in my head, and I'm on narcotics (hydrocodone) because of 3 root canals I just got done and so my brain isn't cooperating with my will to solve this problem.

I think I can figure out (1.) and (2.), but (3.) seems more enigmatic. Maybe it will be easier to grasp once I have (1.) and (2.) working?
I guess my main question is whether or not you agree that a program that does (1.), (2.), and (3.) will satisfy the requirements, and whether or not you have any suggestions on improving (1.), (2.), and (3.) or implementing them.

Thank you.

Alright, here's what I've got so far. Be warned that it doesn't do much of anything and is a half finished amalgam of all of my ideas so far. I would call this a pre-pre-Alpha version, and I didn't include it because I think my original post contained more logical merit:
CODE
    public int recursive(int r){
        
            if( r == 0){  return 0;  }
            else{
                for(int x = r; x > 1; x--){
                if(check[x]){
                    System.out.println("Enter rabbit: n=" + x);
                    check[x] = false;
            }
                else if(!check[r]) {
                    System.out.println("Leave rabbit: n=" + x + " value=");
                    check[x] = true;
            }
            }
             recursive( r - 1);  
            }
         return 0;  
    }

P.s. check[] is a boolean array that is supposed to tell the program to print out "Entering" the first time a rabbit is called, and "Leaving" the next time.


i think that you have to change recursive(r-1) to :
r--;
recursive( r );
because if r is 4 for exam, r-1 is alwayes 3 and recursive(r-1) will take a parameter whic alwayes is 3

This post has been edited by dato.java: 16 Feb, 2008 - 10:36 AM
User is offlineProfile CardPM
+Quote Post

tex1ntux
RE: Need Some Help With Recursive Method
17 Feb, 2008 - 02:03 AM
Post #4

New D.I.C Head
*

Joined: 16 Feb, 2008
Posts: 14


My Contributions
I updated the first post with the latest version of my code.

I went back and reviewed the recursive stuff I did in c++ last year, which helped a little, but I'm getting the feeling I'm missing 1 line of code. sad.gif


UPDATE: This code seems to have much more promise;

UPDATED AGAIN: This code may suck, but technically it is recursive and it does output what it is supposed to when it is given '4'. (btw: 'value' = value user inputs that rabbit() is initially called with)

CODE
public int rabbit(int r){
        if( r == 0) {
            System.out.println("Leave rabbit: n=" + value + " value=" + (value - 1));
            return 1; }
        else if( r == 1){
            System.out.println("Enter rabbit: n=" + (value - 2));
            System.out.println("Leave rabbit n=" + (value - 2) + " value=" + (value - 3)); }
        else if( r == 2){
            System.out.println("Leave rabbit n=" + (value - 1) + " value=" + (value - 2)); }
        else if( r == 3){
            System.out.println("Enter rabbit: n=" + (value - 2));
            System.out.println("Leave rabbit n=" + (value - 2) + " value=" + (value - 3));
            System.out.println("Enter rabbit: n=" + (value - 3));
            System.out.println("Leave rabbit n=" + (value - 3) + " value=" + (value - 3)); }        
         else if( r == 4){
            System.out.println("Enter rabbit: n=" + value );
            System.out.println("Enter rabbit: n=" + (value - 1) ); }
                  
        return rabbit( r - 1);
        
      }


I don't like this solution at all, but I figured writing it out like this might help me visualize how to write a more general solution that is less specific. I'm going to spend the next few days trying to simplify this into a shorter, truly recursive solution instead of this sham of a recursive method. sad.gif

Any tips about how to do that would help, too. I've been reading a bunch of recursion tutorials but I just can't get it to click, if you know what I mean. I don't want anyone to write my code (that would ruin the fun of solving the puzzle smile.gif), but I'm kind of stuck in the mud and my tires are spinning out and I need someone to give me a shove in order to get moving. That, or 4 wheel drive.

Also, dato.java: recursive ( r - 1 ) for r = 4 would call recursive ( 3 ), which would call recursive ( 2 ), etc.

This post has been edited by tex1ntux: 17 Feb, 2008 - 11:43 PM
User is offlineProfile CardPM
+Quote Post

tex1ntux
RE: Need Some Help With Recursive Method
17 Feb, 2008 - 11:50 PM
Post #5

New D.I.C Head
*

Joined: 16 Feb, 2008
Posts: 14


My Contributions
By now I think it is clear that I do not mind completely scrapping an idea and starting over (the code/updates I've posted here are just a few out of dozens), so don't be afraid to say something just because it will invalidate everything I've done so far.

I know I'm doing something wrong solely because of how long this is taking.
I feel like I'm trying to do multiplication using only addition.
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Need Some Help With Recursive Method
18 Feb, 2008 - 12:01 AM
Post #6

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
Do you mind posting the original question from your assignment?

Are you sure that you posted the exact expected output? I have my reservations because it looks a bit strange.
User is offlineProfile CardPM
+Quote Post

tex1ntux
RE: Need Some Help With Recursive Method
18 Feb, 2008 - 12:58 AM
Post #7

New D.I.C Head
*

Joined: 16 Feb, 2008
Posts: 14


My Contributions
QUOTE(Nayana @ 18 Feb, 2008 - 01:01 AM) *

Do you mind posting the original question from your assignment?

Are you sure that you posted the exact expected output? I have my reservations because it looks a bit strange.

IPB Image

The whole class is having trouble with it; the professor extended the due date a week and simplified the original directions (ignore the part about indentation based on level) because no one was able to do it.

This post has been edited by tex1ntux: 18 Feb, 2008 - 01:00 AM
User is offlineProfile CardPM
+Quote Post

tex1ntux
RE: Need Some Help With Recursive Method
20 Feb, 2008 - 10:16 AM
Post #8

New D.I.C Head
*

Joined: 16 Feb, 2008
Posts: 14


My Contributions
Figured it out! icon_up.gif

I was nowhere close before, but luckily the exam we did yesterday had a recursive method we had to analyze and the output was similar enough to this one that I was able to figure it out. Here's the solution I came up with:

CODE
    public int rabbit(int n){
        if(n == 1) {
            System.out.println("Enter rabbit:" + n);
            System.out.println("Leave rabbit:" + n + " value=" + n);
        }
        else if(n == 2) {
            System.out.println("Enter rabbit:" + n);
            System.out.println("Leave rabbit:" + n + " value=" + (n-1));
        }
        else if(n > 2){
            System.out.println("Enter rabbit:" + n);
            rabbit(n-1);
            rabbit(n-2);
            System.out.println("Leave rabbit:" + n + " value=" + (n-1));
        }
        return 0;
    }


This post has been edited by tex1ntux: 20 Feb, 2008 - 10:30 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:23PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month