Results 1 to 5 of 5
  1. #1
    khdani is offline Member
    Join Date
    Aug 2008
    Posts
    14
    Rep Power
    0

    Default java recursion help

    hello,
    i've a recursion function, the problem is that when the recursion ends and begins to return it doesn't keeps the values of the arrays it transforms from one function call to another.

    Java Code:
    	public static boolean solveHelper(int[][] phi, int[] mu, int[][] backup_phi) {
    		boolean satRes=false;
    		int literal;
    				
    				
    		for(int i=0;i<phi.length;i++) {				
    			if(phi[i].length==1) {
    				literal=phi[i][0];
    				int[] temp_arr = new int[mu.length+1];
    				for(int j=0;j<mu.length;j++) {
    					temp_arr[j]=mu[j];
    				}
    				temp_arr[mu.length]=literal;
    				phi=substitute(literal, phi);
    				mu=temp_arr;
    				i=0;
    			}
    		}
    		
    		
    		literal=selectLiteral(phi);
    		boolean allEmpty=false;
    		for(int i=0;i<phi.length;i++) {
    			if(phi[i].length != 0) {
    				allEmpty=true;
    			}
    		}
    		
    		if(literal==0 || allEmpty==false) {
    			if(satisfies(mu, backup_phi)) {
    				return !satRes;
    			}
    			else {
    				return satRes;
    			}
    			
    		}
    		else {
    			int[] new_mu = new int[mu.length+1];
    			for(int i=0;i<mu.length;i++) {
    				new_mu[i]=mu[i];
    			}
    			new_mu[mu.length]=literal;
    			phi=substitute(literal, phi);
    			mu=new_mu;
    			
    			return solveHelper(phi, mu,backup_phi);
    		}
    }
    when the recursion ends, the array mu doesn't have the values it has at the last iteration of the recursion, please help with that.
    Thank You

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    "mu" is changed in the method and/or sent through as a local variable/method argument.
    Try either keeping/declaring "mu" as a member variable, ie, outside the scope of the method, or returning it from your solveHelper method.

  3. #3
    khdani is offline Member
    Join Date
    Aug 2008
    Posts
    14
    Rep Power
    0

    Default

    I see, i must keep mu for the whole recursion lifetime without changing its refference but only values, right ?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Yes, that's one way

  5. #5
    khdani is offline Member
    Join Date
    Aug 2008
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Recursion depth in java
    By poulius in forum New To Java
    Replies: 17
    Last Post: 01-11-2009, 02:38 PM
  2. java program for recursion
    By oshiru in forum New To Java
    Replies: 4
    Last Post: 11-27-2008, 05:34 AM
  3. Recursion in Java ..
    By Java01 in forum New To Java
    Replies: 6
    Last Post: 10-24-2008, 12:42 PM
  4. i could not get the recursion in java
    By sivasayanth in forum New To Java
    Replies: 3
    Last Post: 04-23-2008, 09:08 AM
  5. Recursion in java
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 07:23 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
  •