Results 1 to 7 of 7
  1. #1
    zlwilly is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default Recursion and arrays

    My mind is slowly becoming more open to the concept of recursion, but any suggestion as to how to go about the following would be greatly appreciated.

    I want to make a recursive method that is given an integer array and is able to tell me whether or not it is the same front to back. IE. If given a size 7 array with integers 1 3 5 5 3 1 then it will return true, else it will return false.

    I just need some help nailing down the thought process behind the recursion. I've done simple ones such as counting the frequency of a single digit inside a a larger integer and returning the final number, but I'm at a loss as how to pull this one off..

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Which way would be best? check the outer two numbers first or the inner numbers?

    Once you figure that out, then answer these questions:
    • so you check the two numbers decided on above, now you have to check a subset -- how to get the subset?
    • what should the method return?
    • what are the ending conditions?
    Last edited by Fubarable; 02-05-2009 at 04:48 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Well, I guess you're either not interested in answering my questions, or you've walked away from the computer. Can't say that I didn't try. Good night and much luck.

  4. #4
    zlwilly is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    So sorry, I've been away for a bit. Thanks for getting my mind started. Doing the two outer numbers seems like it would be difficult as they are so far apart. I can't get my head around recursion that could check the two opposite ends of the 'spectrum', I guess. So I'll start thinking about how to check internally and moved outward.

    If the method is going to return a true or false, then the base case would have to do the same, wouldn't it? Just with what should be the final possible thing that could happen..

    Any other suggestion would be great, but I think I'm going to tackle this in the morning, I've been maxxed out for the night, it's a little late here. :] Thanks so much!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Doing the two outer numbers seems like it would be difficult as they are so far apart.
    Distance doesn't mean much here. Which numbers are easier to get a handle on, the outer items which have indices 0 and length - 1, or the inner which have indices, hmmm, let me see, is it an even or odd array, let me think this out algebraically,....

    In this light, which numbers are easier to retrieve? Ignoring "distance" of course ;)

    I can't get my head around recursion that could check the two opposite ends of the 'spectrum', I guess. So I'll start thinking about how to check internally and moved outward.
    think again.

    Next think of the several ending conditions -- what to do with arrays of 0, 1, 2 items?

    Next look into the Arrays class for a useful method that will help you recurse.

  6. #6
    zlwilly is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    I was able to get it working! Thanks for the suggestions. Here's the code in case anyone has need for it.


    Java Code:
          public static boolean forwardsEqualsBackwards( int[] n, int start, int end) {
              if (n[start]!=n[end])
                  return (false);
              else if (start==end)
                  return (true);
              else if (end==start+1)
                  return (n[end]==n[start+1]);
              else  
                  return (forwardsEqualsBackwards(n, start+1, end-1));
        }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Recursion
    By Mika in forum New To Java
    Replies: 5
    Last Post: 01-04-2009, 01:13 AM
  2. Recursion help
    By rjg_2186 in forum New To Java
    Replies: 1
    Last Post: 01-02-2009, 08:03 AM
  3. Recursion
    By Zosden in forum Algorithms
    Replies: 4
    Last Post: 05-05-2008, 05:49 AM
  4. Recursion
    By bozovilla in forum Advanced Java
    Replies: 3
    Last Post: 01-07-2008, 04:53 PM
  5. Help with recursion
    By scts102 in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 10:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •