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

    Default understanding of a recursive method

    Hi, I'm trying to write a method that checks if the sum of the first row in a multi dimensional array is equal to the sum, in at least one row of in the array.

    Ex. {1,1,1}{3}{4,3,2} should return true.
    Ex. {1,1,1}{2}{2,4} should return false.

    For some reason I'm not getting different results.
    Here is my code:

    Java Code:
    public class Q1{
        public static boolean what (int [] [] a){
            return what ( a,0,0,0,0);
        }
        
        private static boolean what (int [] [] a,int f, int c, int i, int j){
            if (i == a.length-1 && c!=f)
            return false;
            
            if (c == f && i==a.length-1)
            return true;
            
            if (i==0){
                if (j<a[0].length)
                what(a,f+=a[0][j],c,i,j+1);
                what(a,f,c,i+1,0);
            }
            
            if (i<a.length){
                if (j<a[i].length)
                what(a,f,c+=a[i][j],i,j+1);
    
                
        }
        
        return what(a,f,0,i+1,0);
    }
    }
    Thank you.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    First, I've fixed your indentation and spacing. These things matter. True, the compiler doesn't enforce them, and there's not universal agreement as to style, but that doesn't mean that style doesn't matter.
    Java Code:
    public class Q1 {
        public static boolean what(int[][] a) {
            return what(a, 0, 0, 0, 0);
        }
        
        private static boolean what(int[][] a, int f, int c, int i, int j) {
            if (i == a.length - 1 && c != f)
                return false;
            
            if (c == f && i == a.length - 1)
                return true;
            
            if (i == 0) {
                if (j < a[0].length)
                    what(a, f += a[0][j], c, i, j+1);
                what(a, f, c, i+1, 0);
            }
            
            if (i < a.length) {
                if (j < a[i].length)
                    what(a, f, c += a[i][j], i, j+1);
            }
        
            return what(a, f, 0, i+1, 0);
        }
    }
    I haven't analyzed your code, but what jumps out at me are all the calls to what() that ignore the return value -- that just doesn't seem right. Do you want to convince me that you meant to do that?

    -Gary-

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

    Default Thanks Gary for taking a look

    The style is important indeed usually I organize it, maybe it's the agony and frustration of this topic that make me think less about it and more of how to solve it..:)

    I forgot to mention I'm not intending to keep you busy with my homework, basically I'm trying still to grasp this recursion subject so some methods work for me and some doesn't, now this one doesn't.
    The problems I'm coming across are mostly in boolean recursions.
    I could write simple methods but in this one I don't know what went wrong.

    So basically I need some help reinforcing my understanding of this kind of methods.

    Thanks.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Well, it's a bit difficult to explain recursion in the abstract. It has to do with simplifying a problem by breaking it into pieces, most of which are slightly simpler versions of the problem itself, and tending toward some trivial "base case". The classic example is a factorial. The factorial of zero, represented by 0! is defined as 1. The factorial n! of any n greater than zero is defined as n times (n-1)!. So the very definition of factorial is recursive -- it refers to itself (in all cases except 0!).

    When we write a recursive method in Java or any programming language, we want to first check the base case (or cases). If conditions match the base case, we can return a definite result. If they don't, we make some small change in the conditions that will bring us a little bit closer to the base case, and combine that with a call to another instance of the very method we're defining. So for a factorial method, we do something like this:
    Java Code:
            public int factorial(int number) {
                // factorial of a negative is undefined, so we really should
                // check for that and throw an Exception. We're ignoring
                // that for purposes of this illustration.
                if (number == 0) return 1; // the base case
                return number * factorial(number - 1); // the recursive case
            }
    Does that help, or do you want more discussion of your particular problem?

    -Gary-

Similar Threads

  1. XML Parsing : use Recursive method.
    By swetareddy in forum AWT / Swing
    Replies: 2
    Last Post: 01-26-2011, 08:09 PM
  2. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  3. recursive method
    By michail in forum New To Java
    Replies: 0
    Last Post: 01-31-2010, 01:50 PM
  4. Understanding this recursive function
    By LifeWithJava in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 05:26 AM
  5. Recursive Method
    By bluegreen7hi in forum New To Java
    Replies: 5
    Last Post: 11-29-2007, 04:45 AM

Posting Permissions

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