Results 1 to 3 of 3
- 02-12-2011, 06:09 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
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:
Thanks.Java 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; } }
- 02-12-2011, 08:52 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
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, 09:06 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 30
- Rep Power
- 0
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.
Similar Threads
-
Clever glimpse needed with this recursion
By Yakg in forum New To JavaReplies: 14Last Post: 02-10-2011, 09:15 PM -
GUI help. Don t understand
By s0meb0dy in forum AWT / SwingReplies: 2Last Post: 10-27-2010, 09:40 PM -
Trying to understand
By ladykrimson in forum New To JavaReplies: 20Last Post: 10-12-2010, 11:10 PM -
findind it very difficult to understand recursion.
By talper in forum New To JavaReplies: 20Last Post: 05-26-2010, 06:18 PM -
recursion and tail-recursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12-28-2009, 06:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks