Results 1 to 7 of 7
Thread: Recursion and arrays
- 02-05-2009, 04:36 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
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.
-
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.
-
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.
- 02-05-2009, 05:15 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
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!
-
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,....Doing the two outer numbers seems like it would be difficult as they are so far apart.
In this light, which numbers are easier to retrieve? Ignoring "distance" of course ;)
think again.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.
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.
- 02-05-2009, 08:48 PM #6
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
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)); }
-
Similar Threads
-
Recursion
By Mika in forum New To JavaReplies: 5Last Post: 01-04-2009, 01:13 AM -
Recursion help
By rjg_2186 in forum New To JavaReplies: 1Last Post: 01-02-2009, 08:03 AM -
Recursion
By Zosden in forum AlgorithmsReplies: 4Last Post: 05-05-2008, 05:49 AM -
Recursion
By bozovilla in forum Advanced JavaReplies: 3Last Post: 01-07-2008, 04:53 PM -
Help with recursion
By scts102 in forum New To JavaReplies: 1Last Post: 11-19-2007, 10:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks