Possible Duplicate:
Horner's recursive algorithm for fractional part – Java
I am writing a program for Horne’r Algorithm, and I will be honest, I do not have much experience with recursion. I have this method set up to accept a fraction only (there is another method which accepts and returns the whole number) and it will return the result converted from base ‘r’ to base 10. I am unsure why, but the method does not seem to be going through the final iteration. Any suggestions as to what I need to do to correct this problem would be greatly appreciated.
(ex: c = 011, xFinal = 2, i = 2)
Expected answer = .375
Actual answer returned = .75
public static double getHornerFraction(long[] c, int xFinal, int i) {
if (i == 0) {
return ((double)c[i])/xFinal;
}
return (getHornerFraction(c, xFinal, i-1) + c[i])/xFinal;
}
From looking at what you specified and what you expect, I think the problem is that you are walking the array
cin the wrong direction or otherwise specifying it incorrectly. I think that what you want to do is actually walk the array from index0toc.length.Call the above function with
c = {0,1,1}, xFinal = 2, i = 0and it should give what you expect.