Help needed trying to understand recursion.

• 02-12-2011, 07:09 PM
Yakg
Help needed trying to understand recursion.
Hi, I am trying to write a recursive method that returns the row of a multi dimensional array with the greatest sum.

For ex, With the given values : {{2,3,4},{6,8,9}{9,9,9,9}}.
Number 3 will be returned as the 3rd row has the largest amount of numbers.

The problem: I keep getting 0 as the result would appreciate if you could help with this method and a good explanation about the procedures as I would like to understand it.

What I've tried to do is go thorough each row and when the sum is the higher than the previous row update the current row.

The method:
Code:

```public int maxRow(){   return maxRow (_mat,0,0,0,0,0); } private int maxRow(int [] [] matrix, int i, int j, int sum,int ezer,int row){       if (j<matrix[i].length-1)           maxRow(matrix,i,j+1,sum,ezer+=matrix[i] [j],row);           if (ezer>sum)           maxRow(matrix,i,j,sum=ezer,0,i);                   if (i<matrix.length-1){               maxRow(matrix,i+1,0,sum,0,i);         }       return row; } }```
Thanks.
• 02-12-2011, 09:52 PM
Yakg
I might reached to an understanding
please hold on with the replies, I've found a similar method on-line and working on it right now.

Thanks.
• 02-12-2011, 10:06 PM
codesmuggler
Why you want to use recursion when the problem is very simple iteration problem?
Don't mess with recursion when it not simplifying the problem.