return doesn't return

tium
tium used Ask the Experts™
on
i have a recursive method that give me an error.

no return!! blah...

look:

public int recursiveMinimum(int[] arr, int size){
 if (size == 1)
    return arr[size];
  else {
   if (arr[size] < (recursiveMinimum(arr, size-1)))
      return arr[size];
  }
}

this method accept an integer array and an integer size of array.  the purpose of this method is to find the minimum value of an element in the array and return to its name.  can somebody tell me why it gives me an error

no return statement.

which i think my pseudocode is clear enough for me to understand.  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
there is a path that does not return anything:
if (size == 1)
   return arr[size];
else {
  if (arr[size] < (recursiveMinimum(arr, size-1)))
     return arr[size];
// else - does not return ???  
 }
}

Commented:
the procedure sould be:

if (size == 1)
   return arr[size];
else {
  // not sure whether the array elements are int
  int min = recursiveMinimum(arr, size-1);
  if (arr[size] < min)
     return arr[size];
  else
     return min;
 }
}

Top Expert 2016

Commented:
Listening...
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Commented:
There should be return statement for if none of your two conditions are achieved
Commented:
public class Min {

  public static int getMin(int[] arr) {
    return(recurseMin(arr, arr.length - 1, Integer.MAX_VALUE));
  }

  public static int recurseMin(int[] arr, int pos, int min_reference) {
//    System.out.println(arr[pos] + " - " + min_reference);
    if(pos == 0) {
      return (arr[pos] < min_reference) ? arr[pos] : min_reference ;
    } else {
      if(arr[pos] < min_reference) {
        min_reference = recurseMin(arr, pos - 1, arr[pos]);
      } else {
        int tmp = recurseMin(arr, pos - 1, min_reference);
        min_reference = (tmp < min_reference) ? tmp : min_reference;
      }
      return(min_reference);
    }
  }

  public static void main(String[] args) {
    int[] arr = new int[] {-100, 200, 1, 3, 5, 7, 1000};
    System.out.println("Ex 1 - Min = " + getMin(arr));
    arr = new int[] {453, -100000, -256, 45, 78, -100, 200, 1, 3, 5, 7, 1000};
    System.out.println("Ex 2 - Min = " + getMin(arr));
  }
}
Ovi

Commented:
But anyway, why do you want to recurse an array, when you can do'it in the clasic way? For bigger arrays this aproach will be slower since is known that recursion needs method context saved/changed at every recursive call.

About your method, see the comments :

public int recursiveMinimum(int[] arr, int size){
if (size == 1) // what is happening with element 0 of the array?
   return arr[size]; //are you sure that is the minimum? How, you have no history of a previously detected minimum value ...
 else {
  if (arr[size] < (recursiveMinimum(arr, size-1)))
     return arr[size];
  // else what's happend if not lower ?
 }
Top Expert 2016

Commented:
Be kinder to your vm :-)

public class ArraySmallest {
  final static int[] numbers = { 1,3,9,20,6,0,20,2,6,7 };

  public static void main(String[] args){
    System.out.println(getIxSmallest(numbers));
  }

  public static int getIxSmallest(int[] numbers){
    int n = Integer.MAX_VALUE;
    int ix = -1;
    for(int i= 0;i < numbers.length;i++){
      if (numbers[i] < n){
        n = numbers[i];
        ix = i;
      }
    }
    return ix;
  }
}

Author

Commented:
thanks for all of your comment.

but i already made the same solution what Venci75 did.  but it gave me array out of bound error.

Author

Commented:
thanks for all of your comment.

but i already made the same solution what Venci75 did.  but it gave me array out of bound error.

Commented:
That is because the array indexes start from 0

if (size == 1)
  return arr[size-1];
else {
 int min = recursiveMinimum(arr, size-1);
 if (arr[size-1] < min)
    return arr[size-1];
 else
    return min;
}
}

Author

Commented:
you mean to say, that i'll do this code

int minimunValue = recursiveMin(anArray,anArray.length- 1);
System.out.println("minimunValue : " + minimunValue);

subtracting 1 to anArray.length.  but take the comment of OVI.

// what is happening with element 0 of the array?
//are you sure that is the minimum? How, you have no history of a previously detected minimum value ...

and if i initialize the array like this.

int[] anArray = { 1000 );

an array with one element w/c have one index of 0.
it will give stackoverflow error.

but recursive method should return 1000 because the size is 1.

right?

Commented:
int[] anArray = {};
have
size=1
one element with index 0



Commented:
sorry - I made a mistake - shoud be:

int[] anArray = {1000};
have
size=1
one element with index 0



Ovi

Commented:
I believe you will never encounter this problem with my solution.

Author

Commented:
hey Ovi,  i got it. look what i got when not using your method.  i use the method of Venci75.  it doesn't return any error at all.

if (size == 0)  <<< change it to zero
 return arr[size-1];
else {
  int min = recursiveMinimum(arr, size-1);
  if (arr[size-1] < min)
     return arr[size-1];
  else
    return min;
  }
}



Ovi

Commented:
if (size == 0)  <<< change it to zero
return arr[size-1];

this will throw a ArrayIndexOutOfBounds exception for 0 size array. Try this :

public class Min1 {
  static {
    int[] a = new int[0];
    recurseMin(a, a.length);
  }

  public static int recurseMin(int[] arr, int size) {
    if (size == 0)  //<<< change it to zero
      return arr[size-1];
    else {
      int min = recurseMin(arr, size-1);
      if (arr[size-1] < min)
        return arr[size-1];
      else
        return min;
    }
  }
}
Ovi

Commented:
Even my method is not prepared for that ...
Should be changed to:
 public static int getMin(int[] arr) {
   if(arr == null || arr.length == 0)
     throw new IllegalArgumentException("Array cannot be null or 0 length");
   return(recurseMin(arr, arr.length - 1, Integer.MAX_VALUE));
 }

Author

Commented:
but ovi it works, try it it, i'll give the same example you made.
[pre]
class RecursiveMethods { // begin RecursiveMethods
  public int recursiveMin(int[] arr, int size) { // begin recursiveMin
    if (size == 0)
       return arr[size];
    else { // begin else
      int min = recursiveMin(arr, size-1);
      return (arr[size] < min) ? arr[size] : min;
    } // end else
  }  //end recursiveMin
  public int recursiveMax(int[] arr, int size) { // begin recursiveMax
    if (size == 0)
      return arr[size];
    else { // begin else
      int max = recursiveMin(arr, size-1);
      return (arr[size] > max) ? arr[size] : max;
    } // end else
  }  //end recursiveMax
} // end class RecursiveMethods

public class Recursive { // begin class
    public static void main(String[] args){ // begin main
      int[] anArray = new int[] {1100, 230, 346, 234, 1256};
      RecursiveMethods recur = new RecursiveMethods();
      System.out.println("Ex 1 min: " + recur.recursiveMin(anArray, anArray.length -1 ));
      System.out.println("Ex 1 max: " + recur.recursiveMax(anArray, anArray.length -1 ));
      anArray = new int[] {453, -100000, -256, 45, 78, -100, 200, 1, 3, 5, 7, 1000};
      System.out.println("Ex 2 min: " + recur.recursiveMin(anArray, anArray.length -1 ));
      System.out.println("Ex 2 max: " + recur.recursiveMax(anArray, anArray.length -1 ));
      anArray = new int[]{453};
      System.out.println("Ex 3 min: " + recur.recursiveMin(anArray, anArray.length -1 ));
      System.out.println("Ex 3 max: " + recur.recursiveMax(anArray, anArray.length -1 ));
    } // end main
} // end class
[/pre]

i did some addition if it really works, i added recursiveMax, with the same technique.

p.s.
i'm not saying that your code is bad, actually it's excelent, i just experimenting! :)
Ovi

Commented:
You have posted this few comments ago :

if (size == 0)  <<< change it to zero
return arr[size-1]; // THIS WILL THROW EXCEPTION
else {
 int min = recursiveMinimum(arr, size-1);
 if (arr[size-1] < min)
    return arr[size-1];
 else
   return min;
 }
}


The code posted by you in your last comment is working fine because the syntax was cleared up, and instead of returning arr[size - 1] you return arr[size].

Author

Commented:
oh, i'am sorry, i just cut and paste the sample code with out using the class type. but have you tried the complete code i posted.  actually i notice your technique

if(pos == 0) { <<< this one

i remember that an array has intial index of 0, so i change that.  how stupid i'am.  very well, thanks a lot.  for your patience.

teng    

p.s.
do you know any site pertaining to Swing,  i don't like the Swing tutorials of java sun.  it's to general, i want to learn it by details.  i have download some swing tutorials but still i can't grasp the idea.  thanks again. master!
Ovi

Commented:
Try this link (to the bottom of the page) :

http://manning.spindoczine.com/sbe/

Author

Commented:
this book what i pertaining for
>>i have download some swing tutorials but still i can't grasp the idea

thanks any way.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial