Results 1 to 4 of 4
 03022011, 05:11 PM #1Member
 Join Date
 Dec 2010
 Posts
 60
 Rep Power
 0
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.length1 && c!=f) return false; if (c == f && i==a.length1) 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); } }
 03022011, 05:39 PM #2Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 7
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); } }
Gary
 03022011, 05:58 PM #3Member
 Join Date
 Dec 2010
 Posts
 60
 Rep Power
 0
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.
 03022011, 07:03 PM #4Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 7
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 (n1)!. 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 }
Gary
Similar Threads

XML Parsing : use Recursive method.
By swetareddy in forum AWT / SwingReplies: 2Last Post: 01262011, 09:09 PM 
Java class HashIt with a static recursive method and a static iterative method
By kezkez in forum New To JavaReplies: 3Last Post: 02092010, 06:22 AM 
recursive method
By michail in forum New To JavaReplies: 0Last Post: 01312010, 02:50 PM 
Understanding this recursive function
By LifeWithJava in forum New To JavaReplies: 3Last Post: 12302008, 06:26 AM 
Recursive Method
By bluegreen7hi in forum New To JavaReplies: 5Last Post: 11292007, 05:45 AM
Bookmarks