1. Member
Join Date
Aug 2008
Posts
14
Rep Power
0

## 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. "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. Member
Join Date
Aug 2008
Posts
14
Rep Power
0
I see, i must keep mu for the whole recursion lifetime without changing its refference but only values, right ?

4. Yes, that's one way

5. Member
Join Date
Aug 2008
Posts
14
Rep Power
0
I fixed it,
Thank You

#### Posting Permissions

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