d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > [java] Finding Maximum Of An Array - Recursively
Add Reply New Topic New Poll
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Jun 1 2013 05:37am
the task is to use recursion to find the maximum of an integer array.
the skeleton of the method looks like this, it has to take 2 parameters.

Code
   public static int max(int[] array, int index) {
   //stuff
}
   


I'm lost on this one. Any hints? I can do it with three parameters, writing a max(int[] array, int index, int max) method, but not with two parameters.
Member
Posts: 1,967
Joined: Jul 10 2007
Gold: 1,252.00
Jun 1 2013 08:05am
Assuming that index represents the index of the element to read, the pseudo code would be something like the following:
Code

public static int max(int[] array, int index) {
  // If index == last element of the array, return array[index]
  // Else compare array[index] to max(array,index+1]), return the greater of the two.
}
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Jun 1 2013 03:02pm
I came up with something before reading your reply, I guess this is what you meant?

Code
public static int max(int[] array, int index) {
       if(index==array.length-1) {
           return array[index];
       } else if(array[index]>array[index+1]) {
           array[index+1]=array[index];
       }
       return max(array,index+1);        
   }


This post was edited by tt_toby on Jun 1 2013 03:03pm
Member
Posts: 1,967
Joined: Jul 10 2007
Gold: 1,252.00
Jun 1 2013 10:38pm
If you read through the code that you wrote you will see that your code isn't in line with the pseudo code I provided but, i guess it satisfies the requirement that the method return the max value for an array. But the way that you are going about it you are modifying the contents of the input array.

Code


class Main
{
   public static int max(int[] array, int index) {
      if(index==array.length-1) {
          return array[index];
      } else if(array[index]>array[index+1]) {
          array[index+1]=array[index];
      }
      return max(array,index+1);        
  }
 
       public static void main (String[] args)
       {
               int [] myArray = {1,3,2,4,5,1};
               System.out.println ("--- Initial Values ---");
               int j = 0;
               for(int i : myArray){
                    System.out.println("Element: " + j + ", Value: " + i);
                    j++;
               }
               System.out.println ("--- Max Value ---");
               System.out.println (max(myArray,0));
               System.out.println ("--- Final Values ---");
               j = 0;
               for(int i : myArray){
                    System.out.println("Element: " + j + ", Value: " + i);
                    j++;
               }
       }
}


--- Initial Values ---
Element: 0, Value: 1
Element: 1, Value: 3
Element: 2, Value: 2
Element: 3, Value: 4
Element: 4, Value: 5
Element: 5, Value: 1
--- Max Value ---
5
--- Final Values ---
Element: 0, Value: 1
Element: 1, Value: 3
Element: 2, Value: 3
Element: 3, Value: 4
Element: 4, Value: 5
Element: 5, Value: 5

Notice the difference in the values before and after max() is invoked.




Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Jun 2 2013 10:44am
Alright, I translated your pseudo-code and ofc it works. But I have a hard time grasping what's happening / what the general idea is.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 2 2013 11:36am
Quote (tt_toby @ Jun 2 2013 12:44pm)
Alright, I translated your pseudo-code and ofc it works. But I have a hard time grasping what's happening / what the general idea is.


draw out your stack to see what's happening.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll