Results 1 to 3 of 3
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default 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:
    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;
    }
    }
    Thanks.

  2. #2
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default 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.

  3. #3
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    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

  1. Clever glimpse needed with this recursion
    By Yakg in forum New To Java
    Replies: 14
    Last Post: 02-10-2011, 09:15 PM
  2. GUI help. Don t understand
    By s0meb0dy in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2010, 09:40 PM
  3. Trying to understand
    By ladykrimson in forum New To Java
    Replies: 20
    Last Post: 10-12-2010, 11:10 PM
  4. Replies: 20
    Last Post: 05-26-2010, 06:18 PM
  5. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •