Home > Back-end >  Which recursive method could be used to evaluate numbers in a list?
Which recursive method could be used to evaluate numbers in a list?

Time:12-07

I'm strugling learning recursive methods. Could use some advice and a cheer-up :D

Lets say I have a set of numbers in an array, and I want to use a recursive method to evaluate numbers in this set, and by evaluation I mean that our program needs to find zeros in this set, and as a result it should print out position of the last zero in given array.

int a[] = {2,3,4,5,0,1,2,0,5,0};

In case if array above is being evaluated by a recursive method, the program will evaluate each number and, if zero is found, it will save it's position in a variable, AND THEN MOVE ON REPEATING ITSELF until the end of the array. So first zero is at "4" and so on, but the output will show only the poistion number of the last zero in given array.

I kind of got the first part of this code going, but my recursion stops as soon as it finds a zero and just gives out a boolean type variable.

    public static boolean checkNum(int i) {
        if (i < a.length) {
            if (a[i] != 0)
                return checkNum(i 1);
            else return false;
        }
        return true;
    }

this block of code checks numbers in our set and returns boolean type variable true in case if it never found any zeros.

next method may be similar, but I dont know how to store the position number as a variable with a possibility to print it out in the end of my program. Is there any way to get int i out of the recursive method when it's done repeating itself? Because as far as I understand it is the same as a position number in an array, which I need to print out.

CodePudding user response:

You have two options. Either continue until the end of the list and then return the last index found ... or iterate backwards across your list and use your logic as-is.

You're currently iterating forward, so might as well cover that workflow first.

  • Since your goal is to figure out the position, your method needs to return an int, not a boolean.
  • Since you need to return the last index of a zero, that means you need to track the previously found index.
  • Since you want to iterate to the end of the list, your end condition needs to be a position < length check.
static int[] a; // some array we're checking

public static int checkNum(int index, int lastZero) {
    // End condition. If we've finished iterating across the array, return the 
    // index of the last zero we found.
    if (index >= a.length) {
        return lastZero;
    }

    // Check if we've found a zero at the current position.
    if (a[index] == 0) {
        lastZero = index;
    }

    // Continue traversing the list.
    return checkNum(index 1, lastZero);
}

You'd call this by passing in a negative number as your initial lastZero index:

int lastZeroIndex = checkNum(0, -1);

That way you know that if you end up with a positive number, you have found the last index. Otherwise if you're left with a negative number, there were no zeroes in the array.


A simpler method would simply be to iterate backwards over the array and stop as soon as you find a zero.

  • We still return an int instead of a boolean because we want to know the index.
  • Otherwise your method signature remains the same.
static int[] a; // some array we're checking

public static int checkNum(int index) {
    // End condition. We've reached the beginning of the array and never found a zero.
    // Return a -1 to indicate this.
    if (index < 0) {
        return -1;
    }

    // If index is >= a.length, we don't want an index out of bounds...
    if (index >= a.length) {
        return checkNum(a.length - 1);
    }

    // Check if we've found a zero at the current position. If we have, return
    // the current index.
    if (a[index] == 0) {
        return index;
    }

    // Continue traversing the list backwards.
    return checkNum(index-1);
}

You'd call this by passing a.length - 1 as your parameter. That's the last valid index in the array, so it's our starting point when we iterate backwards.

int lastZeroFound = checkNum(a.length - 1);

If lastZeroFound is negative (eg. -1), then you found no zeroes.

  •  Tags:  
  • java
  • Related