Recursive base conversion - unexpected output
Author |
Message |
Srlancelot39

|
Posted: Thu Nov 01, 2012 3:05 pm Post subject: Recursive base conversion - unexpected output |
|
|
I need to create code for a recursive method that converts the base of a number. The return function was not working as I expected so I got the method to print out the variable values and discovered that my algorithm is correct, but the return function is outputting something different.
The method:
code: | public String convert(int number, int base, String result)
{
int quotient = number/base;
int remainder = number%base;
result = Integer.toString(remainder) + result;
System.out.println("N: " + number + " Q: " + quotient + " R: " + result);
if(quotient != 0)
{
convert(quotient, base, result);
}
return result;
} |
The output with 30 as initial number and 4 as base to be converted to:
Quote: N: 30 Q: 7 R: 2
N: 7 Q: 1 R: 32
N: 1 Q: 0 R: 132
Result is: 2
As you can see, the final value of R (result) is 132 but return is returning "2".
...why? O_O
EDIT: sorry for all the edits. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
TerranceN
|
Posted: Thu Nov 01, 2012 9:47 pm Post subject: RE:Recursive base conversion - unexpected output |
|
|
The problem with what you're expecting to happen lies in this line:
Java: |
result = Integer.toString(remainder) + result;
|
Remember that every variable is a reference in Java. When you call convert, result is set to reference the same thing as the reference you passed in. Then on the line above, since String's are immutable, the reasonable thing for Java to do is set result to reference the result of Integer.toString(remainder) + result.
I may have explained that poorly, so here's an example of the problem
Java: |
public class Test {
private static void strFunction(String s) {
s = "asdf";
}
public static void main(String[] args) {
String q = "qwerty";
strFunction(q);
System.out.println(q); // outputs qwerty
}
}
|
Fortunately for you, you already set up convert to return result so you can get the recursive result directly from the recursive call instead of relying on the recursive call to change the value you pass in. |
|
|
|
|
 |
Srlancelot39

|
Posted: Fri Nov 02, 2012 1:25 pm Post subject: RE:Recursive base conversion - unexpected output |
|
|
Okay, that sounds like it's most likely what my problem is, but then why is system.out.println showing the desired value of 'result' and return result isn't. There are no changes made to result between the two outputs :s
Assuming what you outlined is the problem, how would I go about fixing this? |
|
|
|
|
 |
Srlancelot39

|
Posted: Fri Nov 02, 2012 2:47 pm Post subject: Re: Recursive base conversion - unexpected output |
|
|
Redesigned the code and fixed the problem!
New, working code:
code: | public String convert(int number, int base)
{
int quotient = number/base;
int remainder = number%base;
if(quotient != 0)
{
return ((convert(quotient, base) + Integer.toString(remainder)));
}
else
{
return (Integer.toString(remainder));
}
} |
|
|
|
|
|
 |
|
|