2

I have been trying to print elements in

int[][]a= {{2,-36,98},{21,55},{2,5,4,7,6},{101}}

with the help of recursion instead of a loop. Now i have a piece of code with me but it prints extra unwanted elements.

public class RecursionDoubleLoop {
    void loop(int[][]a,int x,int y)
    {
        int n=a.length;
        if(x<n)
        {

            if(y<a[x].length)
            {

                System.out.println(a[x][y]+" ");

                y+=1;
                if(y<a[x].length)
                loop(a, x, y);
            }
            y=0;
            x+=1;
            /*if(x>=n)
            {
                System.exit(0);
            }*/
            if(x<n) {
            loop(a, x, y);}
        }
    }
    public static void main(String[] args) {
        RecursionDoubleLoop obj= new RecursionDoubleLoop();
        int[][]a= {{2,-36,98},{21,55},{2,5,4,7,6},{101}};
        obj.loop(a, 0, 0);
    }

}

Now the Expected Output is

2 -36 98 21 55 2 5 4 7 6 101

My output

2 -36 98 21 55 2 5 4 7 6 101 101 101 101 101 2 5 4 7 6 101 101 101 101 101 21 55 2 5 4 7 6 101 101 101 101 101 2 5 4 7 6 101 101 101 101 101 21 55 2 5 4 7 6 101 101 101 101 101 2 5 4 7 6 101 101 101 101 101

Tried debugging but ultimately had to uncomment the System.exit(0) function.

It will be very helpful if someone can point out the error.

3 Answers 3

2

You're so close that it hurts to tell you the solution. All you need to do is simply return in the check for y < a[x].length. This is because the recursive call to loop is going to increase y until this is false and ultimately proceed to increase x. So close man.

if (y < a[x].length) {
    loop(a, x, y);
    return;
}

Output

2 
-36 
98 
21 
55 
2 
5 
4 
7 
6 
101 
Sign up to request clarification or add additional context in comments.

4 Comments

Can u explain it a bit more coz I am a beginner and need some more knowledge regarding this.
Absolutely. Is there are particular part you're stumped on? Is it regarding the use of return?
It is regarding the use of return and also why does my code didnt not work ? i mean i have used the if conditions correctly still :(
The code you provided didn't work because after each call to loop subsequently finished, the x was always being incremented and if x was less than n, it would loop again. By using return when looping through y, we don't increment x. By doing so, on the next call to loop, x will be incremented as intended. I hope this helps.
2

You need to wrap your inner code in an else block when going to the next row. Also, a few checks are not needed there, since they will be caught by recursion.

void loop(int[][] a, int x, int y) {
    int n = a.length;
    if (x < n) {
        if (y < a[x].length) {
            System.out.print(a[x][y] + " ");
            loop(a, x, y + 1);
        } else {
            loop(a, x + 1, 0);
        }
    }
}

How I figured out? I printed "loop " + x + " " + y on the first line of the method to figure out what is wrong with your code. It was easy to see that your implementation was good but it was not stopping when increasing x.

If you want a compart implementation(basically, inverted if conditions), you can try this:

void loop(int[][] a, int x, int y) {
    if (x >= a.length) return;
    if (y >= a[x].length) {
        loop(a, x + 1, 0);
    } else {
        System.out.print(a[x][y] + " ");
        loop(a, x, y + 1);
    }
}

Comments

0

Check if the x reaches the rows limit or y is greater than columns limit then return from the recursion.

, Check if the y reaches the columns limit then start new recursion to get the other arrays with x+1 and y 0 then return from the recursion.

, So the second condition will increase x and the inner recursion will increase y

    void loop(int[][] a, int x, int y) {
        if (x >= a.length || y > a[x].length)
            return;
        if (y == a[x].length) {
            System.out.println();
            loop(a, x + 1, 0);
            return;
        }

        System.out.print(a[x][y] + " ");
        loop(a, x, y + 1);
    }

, output

2 -36 98
21 55
2 5 4 7 6
101

Comments

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.