Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-28-2008, 09:58 PM
Member
 
Join Date: Aug 2008
Posts: 14
Rep Power: 0
khdani is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-28-2008, 11:11 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-29-2008, 02:31 PM
Member
 
Join Date: Aug 2008
Posts: 14
Rep Power: 0
khdani is on a distinguished road
Default
I see, i must keep mu for the whole recursion lifetime without changing its refference but only values, right ?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-29-2008, 08:18 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Yes, that's one way
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-29-2008, 09:56 PM
Member
 
Join Date: Aug 2008
Posts: 14
Rep Power: 0
khdani is on a distinguished road
Default
I fixed it,
Thank You
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursion depth in java poulius New To Java 17 01-11-2009 02:38 PM
java program for recursion oshiru New To Java 4 11-27-2008 05:34 AM
Recursion in Java .. Java01 New To Java 6 10-24-2008 12:42 PM
i could not get the recursion in java sivasayanth New To Java 3 04-23-2008 09:08 AM
Recursion in java lenny Advanced Java 1 08-07-2007 07:23 AM


All times are GMT +2. The time now is 12:19 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org