1

This is the method code:

public static void printMatrix(int[][] m, int i, int j) {
    if (i == m.length ||j==m.length) {
        System.out.println();
    } else {
        System.out.print("[" + m[i][j] + "]");

        printMatrix(m, i, j++);
        printMatrix(m, i++, j);
    }

}

I don´t know why it just prints the first position of the array until a StackOverFlow error.

Thanks for the help.

MQSJ23
  • 107
  • 2
  • 2
  • 6

4 Answers4

2

You call 2 times the recursive function, but it keep calling itself with i and j..

printMatrix(m, i, j++);  << use ++j
printMatrix(m, i++, j); << use ++i

Here is a possible solution for you

public static void printMatrix(int[][] m, int i, int j)
{
    System.out.print("[" + m[i][j] + "]");
    if (i == m.length && j == m.length)
    {
        return;
    }

    if (j == m.length)
    {
        j = 0;
        ++i;
        printMatrix(m, i, j);
    }
    else 
    {
        j++;
        printMatrix(m, i, j);
    }
}

non-recursive

public static void printMatrix(int[][] m)
{
    for (int i = 0; i < m.length; i++)
        for (int j = 0; j < m.length; j++)
            System.out.print("[" + m[i][j] + "]");
}
lordkain
  • 3,061
  • 1
  • 13
  • 18
1

If you're trying to print each element of the matrix once, then none of the solutions in the other answers [EDIT: I guess we're down to one answer now] is going to help. The most they will do is get rid of the stack overflow error, but the output is still not going to be close to what you need.

Assuming this is a homework assignment and you've been told to use recursion for some reason (in real life nobody would do that), you have to step back and think about this: just what do you want printMatrix(m,i,j) to do? Presumably, you want to print the m[i][j] element, and then call printMatrix to print the rest of the matrix. When you call printMatrix recursively to start the printing rest of the matrix, what do you want i and j to be? Probably, you want the same i and the next column, j+1, but not if j is at the end of the row. Then you want ... I'll let you think about that. But I don't think you want printMatrix to call itself twice. Instead, you want it to call itself only once (at most); you'll probably need an if statement that looks something like

if(something) 
   printMatrix(something); 
else 
   printMatrix(something different);

but it will still call itself only once (since it will pick one or the other).

I'll mention one other thing: you're comparing i to the number of rows in the array (m.length), but you're also comparing j to the number of rows in the array. That's fine if you know this is a square matrix. But if you want to compare j to the number of columns, compare it to m[i].length, since m[i] is itself an array (that represents one row of the matrix).

ajb
  • 31,309
  • 3
  • 58
  • 84
0

The size of array 'm' will be constant through out the recursive calls. Whereas the value of of i and j will be changing and base condition will be only satisfied once that. So it infinitely enter the base condition was the same i and j. That is why i guess it keeps printing only one value and after sometime the stack overflows. i do not really view this as proper use of recursion. Correct me if i'm wrong. when using recursion a problem reduces size as function calls are made till it is broken into the smallest unit possible, which can be identified by the base condition or the breaking point. I don't see this happening here.

public class Printarray {
 static int max= 2;
/**
 * @param args the command line arguments
 */

public static void main(String[] args) {
    // TODO code application logic here
     int array[]={1,2,3};
    print(array,0);




}
public static void print(int array[],int i)
{
     System.out.println(array[i]);
    if(i==Printarray.max)
    {
          Printarray.max--;
        return;
    }
    else
    {
        print(array,i+1);
    }

}
} 

This works for 1D array, you can try this for 2D arrays and see if it works. I hope it helps!

0
private static void print(int[][] mat, int i, int j) {
    // TODO Auto-generated method stub
    if(mat==null){
        return;
    }
    if(i==mat.length || j==mat[0].length){
        return;
    }       
    System.out.print(mat[i][j]+" ");
        if(j==mat[0].length-1){
                System.out.println();
            print(mat,i+1,0);
        }       
        print(mat,i,j+1);       
    }
Rahul
  • 21
  • 4