Results 1 to 7 of 7
  1. #1
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default Array - Recursive search.

    Is it possible to search an array for, lets say the biggest (int), without passing the index? I can't think of any way, but if anyone has a nifty trick!?

    Java Code:
       public static int arrayMax(int i, int[] array) 
       {
          if(i == array.length-1)    //i can be used as an offset.
          {
             return array[i];
          }
    
          int temp = arrayMax(i+1, array);    //next index.
    
          if(array[i] > temp)
          {
             return array[i];
          }
          return temp;
       }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There is a way, you should consider having the method take 3 arguments, 1 which is the index, one which is the array and one that's the largest number.
    The termination condition is if index == array.length.

    With that in mind, give it a shot and post up your attempt.

  3. #3
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Pass the number found you say. All right, the code definitely gets easier to read. Any other things on the pro list?

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    use a for-each loop instead?

    finding the largest int:
    Java Code:
    int largestInt = intArray[0];
    for (int nextInt:intArray) {
        if (largestInt < nextInt) {
            largestInt = nextInt;
        }
    }

  5. #5
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    use a for-each loop instead?
    ...
    [code]for (int nextInt:intArray)
    Normally I would use a for loop, but this was a school exercise in which I was supposed to use a recursive function. Further, I was to use arrayMax(index, array[]) as params. (We're testing execute time comparing loops and recursive functions.) However, thanks for the for syntax. New to me. :)

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What have you tried using a 3rd variable? The index should change each recursive pass, and the max variable should only change sometimes. Show your attempts, and Ill let you know where you went right/wrong.

  7. #7
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Show your attempts, and Ill let you know where you went right/wrong.
    See the first post in this thread. It seems to work like a charm. Cheers!

Similar Threads

  1. Array out of bound- Recursive Method
    By hpayandah in forum New To Java
    Replies: 2
    Last Post: 11-12-2010, 08:02 PM
  2. Recursive method using int array, help needed
    By chupalo17 in forum New To Java
    Replies: 4
    Last Post: 09-07-2009, 11:15 PM
  3. Recursive Binary Search
    By EternalSolitude in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 06:26 AM
  4. Replies: 1
    Last Post: 02-16-2008, 09:10 PM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 03:08 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
  •