Home > OS >  Why is my return statement not working in this recursive function?
Why is my return statement not working in this recursive function?

Time:01-21

I have an array which is pre-sorted with only-ascending integers:

int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9 };

I want my program to search for an integer in that array which I'll call "gesucht" and to return the place in the array where "gesucht" is.

I may not use any loops, packages or something like the .find method and have to use recursion

This is my program:

public static int optimierteSuche(int gesucht, int[] array, int startposition, int endposition){
    if((0<=startposition && startposition<=endposition && endposition<=array.length) == false){
        throw new IllegalArgumentException("wrong input");
    }
    int m = (endposition-startposition)/2;
    if (gesucht == array[m]){
        return m;
    }
    if (array[m]<gesucht){
        optimierteSuche(gesucht, array, m 1, endposition);
    } else {
        optimierteSuche(gesucht, array,startposition, m-1);
    }

}

When I use the following in my main Method:

int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9 };
System.out.println(optimierteSuche(2,intArray,0,intArray.length));

My compiler tells me that my method is missing a return statement, although it's there and when I use my algorithm and calculate the steps myself it works (or at least I'd say it does)

I've already tried to adjust it a little, but it didn't work either:

public static int optimierteSuche(int gesucht, int[] array, int startposition, int endposition){
        if((0<=startposition && startposition<=endposition && endposition<=array.length) == false){
            throw new IllegalArgumentException("wrong input");
        }
        int m = (endposition-startposition)/2;
        int ergebnis = 0;
        if (gesucht == array[m]){
            ergebnis = m;
        }
        if (array[m]<gesucht){
            optimierteSuche(gesucht, array, m 1, endposition);
        } else {
            optimierteSuche(gesucht, array,startposition, m-1);
        }
        return ergebnis;
    }

I study economics and take some programming courses because of interest but thus do not have much contact to many others programming majors and would like some help. Thank you!

CodePudding user response:

I think you want to set ergebnis equal to the return value of your two calls to optimierteSuche, and fix your calculation of the middle position, and also add one more 'else', so that it looks like this:

public static int optimierteSuche(int gesucht, int[] array, int startposition, int endposition){
        if((0<=startposition && startposition<=endposition && endposition<=array.length) == false){
            throw new IllegalArgumentException("wrong input");
        }
        int m = startposition   ((endposition-startposition)/2);
        int ergebnis = 0;
        if (gesucht == array[m]){
            ergebnis = m;
        }
        else
        if (array[m]<gesucht){
            ergebnis=optimierteSuche(gesucht, array, m 1, endposition);
        } else {
            ergebnis=optimierteSuche(gesucht, array,startposition, m-1);
        }
        return ergebnis;
    }

You were almost there!

CodePudding user response:

tgdevies is correct, credit to them, but because comments don't always express what needs to happen...

public static int optimierteSuche(int gesucht, int[] array, int startposition, int endposition) {
    if ((0 <= startposition && startposition <= endposition && endposition <= array.length) == false) {
        throw new IllegalArgumentException("wrong input");
    }
    int m = startposition   ((endposition - startposition) / 2);
    if (gesucht == array[m]) {
        return m;
    }
    if (array[m] < gesucht) {
        return optimierteSuche(gesucht, array, m   1, endposition);
    }
    return optimierteSuche(gesucht, array, startposition, m - 1);
}

Runnable example

public class OtherTest {

    public static void main(String[] args) {
        int[] intArray = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        System.out.println(optimierteSuche(2, intArray, 0, intArray.length));
        intArray = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        System.out.println(optimierteSuche(2, intArray, 0, intArray.length));
    }

    public static int optimierteSuche(int gesucht, int[] array, int startposition, int endposition) {
        if ((0 <= startposition && startposition <= endposition && endposition <= array.length) == false) {
            throw new IllegalArgumentException("wrong input");
        }
        int m = startposition   ((endposition - startposition) / 2);
        if (gesucht == array[m]) {
            return m;
        }
        if (array[m] < gesucht) {
            return optimierteSuche(gesucht, array, m   1, endposition);
        }
        return optimierteSuche(gesucht, array, startposition, m - 1);
    }
}
  •  Tags:  
  • Related