0

I am having a problem with converting a while loop to a recursion ... the loop seems to work fine however I tried several times to turn it to recursion and what the method returns is the last (return c;) as 0 ... I mean how can you actually turn a while loop into a recursion? the program should count the numbers below 2 in the array

thats the main

public static void main(String[] args) {

    double[] gpa = new double[]{2.5, 1.3, 1.3, 3.3, 1.2, 3.2, 4, 2.3, 3.1, 1.2};

    int start = 0;
    countGPA(gpa,start);

    System.out.println(countGPA(gpa, start));
}

and thats the method

public static int countGPA(double[] gpas, int start) {
    int L = gpas.length;
    int c = 0;
    int countGPA = c;

    while (start < 10) {

        if (start > 0 && start != L) {
            if (gpas[start] < 2.0 && gpas[start] > 0) {
                c++;
            }
        } else if (start == L) {
            return 0;
        } else if (start < 0 && start > L) {
            return -1;
        }
        start++;
    }

    return c;
}
1
  • 3
    Show us what you tried? Commented May 19, 2020 at 19:16

3 Answers 3

1

This looks like a simple recursion:

public int GPA(double[] gpas, int index){
    if(index >= gpas.length) return 0;

    if(0 < gpas[index] && gpas[index] < 2) return 1 + GPA(gpas, index + 1);
    else return GPA(gpas, index + 1);
}

Just call it GPA(gpa, 1).

There is a lot of unnecessary comparisons in your method. Look at your uses of 10, L and start.


For example, suppose start = 0. No one of your ifs will enter. Better to start with 1. Look:

if (start > 0 && start != L)     //start is 0 so this won't enter

else if (start == L)             //start is 0 so this won't enter

else if (start < 0 && start > L) //start is 0 so this won't enter
Sign up to request clarification or add additional context in comments.

3 Comments

oh i forgot to mention that if start is a negative integer or start is greater than gpa.length it should return -1 also if start is equal to gpas.length it should return 0 any ideas ?
Yes, you can test these specific values inside a specific function, then if no one of these cases apply, you call the recursive option to deal with the rest.
if(start < 0) return -1, else if(start == gpas.length) return 0, else return GPA(gpas, start).
0

There are three most important things about a recursive function/method:

  1. The terminating condition.
  2. The value with which the method/function is called recursively.
  3. Where (before/after the recursive call) to process the parameter(s).

Do it as follows:

public class Main {
    public static void main(String[] args) {
        double[] gpa = new double[] { 2.5, 1.3, 1.3, 3.3, 1.2, 3.2, 4, 2.3, 3.1, 1.2 };
        int start = 0;
        System.out.println(countGPA(gpa, start));
    }

    public static int countGPA(double[] gpas, int start) {
        return countGPA(gpas, start, 0);
    }

    public static int countGPA(double[] gpas, int start, int count) {
        if (start >= gpas.length) {// Terminating condition
            return count;
        }
        if (gpas[start] < 2.0 && gpas[start] > 0) {
            return countGPA(gpas, ++start, ++count);// The recursive call
        } else {
            return countGPA(gpas, ++start, count);// The recursive call
        }
    }
}

Output:

4

Comments

0

Two important things to note when creating recursive methods.

  1. You must include a base case, that when true stops the recursive calls and returns the values. If you don't have a base case, you'll run into a StackOverFlow exception.

In your scenario the recursion will stop when the index value is equal to the length of the array.

  1. The method must call itself from within.

Anything that can be iterated over can also be a candidate for recursion.

Info on recursion: https://www.javatpoint.com/recursion-in-java

public int numbersBelowTwo(double[] gpas, int index){

    //Base case, when this statement equates to true
    //the recursions stops and returns the values.
    if (index == gpas.length) return 0;

    return gpas[index] < 2 ? 1 + numbersBelowTwo(gpas, ++ index) : numbersBelowTwo(gpas, ++index);

}

2 Comments

Bradley - This is a better tutorial.
Thanks, Arvind. 2 resources are better than 1!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.