CRUST01
CRUST01

Reputation: 31

Problem with printing a 2d array recursively

When printing out a 2d array recursively, the method prints out both columns and repeats the second column twice with three extra spacings. How do I fix this?

Entire code:

public static void main(String [] args) {
    final int ROWS = 2, COLS = 2;
    long [][] arr = new long [ROWS][COLS];
    setArray(arr, 0, 0);

    System.out.println("Numbers in 2d Array: ");
    printArray(arr, 0, 0);

}

public static void setArray(long [][] a, int r, int c){
    if(r < a.length){
        if(c < a[r].length){
            a[r][c] = (long)(Math.random() * 100);
            setArray(a, r, ++c);
        }
        setArray(a, ++r, c = 0);
    }
}

public static void printArray(long [][]a, int r, int c){
    if(r < a.length){
        if(c < a[r].length){
            System.out.print(a[r][c] + " ");
            printArray(a, r, ++c);
        }
        System.out.println();
        printArray(a, ++r, c = 0);
    }
}

Method that I am having trouble with:

public static void printArray(long [][] a, int r, int c){
    if(r < a.lenght){
        if(c < a[r].length){
            System.out.print(a[r][c] + " ");
            printArray(a, r, ++c);
        }
        System.out.println();
        printArray(a, ++r, c = 0);
    }
}

Expected output:

Numbers in 2d Array:

74 16

44 91

Actual output:

Numbers in 2d array:

74 16

44 91

44 91

44 91

Upvotes: 0

Views: 1213

Answers (2)

Progman
Progman

Reputation: 19546

The problem is that you are not exiting the printArray() method when you are inside the second if statement printing a single cell value. This means that, when you print the single cell value, you will always execute the System.out.println(); at the end as well. This will result in a lot of new lines and output of parts of an array that doesn't make sense.

When you add the line System.out.println("Called with r="+r+",c="+c); at the beginning of your printArray() method you will get the following output for debugging purposes:

Numbers in 2d Array: 
Called with r=0,c=0
89 Called with r=0,c=1
59 Called with r=0,c=2

Called with r=1,c=0
90 Called with r=1,c=1
71 Called with r=1,c=2

Called with r=2,c=0

Called with r=2,c=0

Called with r=2,c=0

Called with r=1,c=0
90 Called with r=1,c=1
71 Called with r=1,c=2

Called with r=2,c=0

Called with r=2,c=0

Called with r=2,c=0

Called with r=1,c=0
90 Called with r=1,c=1
71 Called with r=1,c=2

Called with r=2,c=0

Called with r=2,c=0

Called with r=2,c=0

As you see by the debug lines, the row and column indices doesn't go up all the time, but instead going back from row index 2 to index 1. This should be an indicator what is wrong with your printArray(); method.

To fix the problem, simply use a return; statement inside your second if() to exit the method early (and not call the System.out.println(); line) or use an if-else statement to print either the cell value OR generate a new line.

public static void printArray(long [][]a, int r, int c){
    if(r < a.length){
        if(c < a[r].length){
            System.out.print(a[r][c] + " ");
            printArray(a, r, ++c);
            return; //  <----------- here
        } // <--- or use an "else" block here
        System.out.println();
        printArray(a, ++r, c = 0);
    }
}

Upvotes: 1

user1736525
user1736525

Reputation: 1129

Looks like a homework question. So I would suggest stepping through the execution to find the issue. If you are still stuck, my hint is to think about when to return.

Upvotes: 0

Related Questions