# understanding of a recursive method

• 03-02-2011, 05:11 PM
Yakg
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:

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.
• 03-02-2011, 05:39 PM
gcalvin
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.
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-
• 03-02-2011, 05:58 PM
Yakg
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.
• 03-02-2011, 07:03 PM
gcalvin
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:
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-