I have to write a program converting from any base to decimal using recursion and Horner's scheme. All I can use is:
- an int method with two parameters like public static int horner (int[] digits, int base){}. I have to implement the method in-place, i.e. I cannot use any other array other than the one given as a parameter.
- a helper method.
How do I do this? All I have is this, but I know it makes no sense. I don't know how to implement a counter so that I could create a terminating base for the recursion.
public static int horner (int[] digits, int base) {
int n = digits.length;
int a = 0;
int decimal = digits[0];
if (n == 0) {
System.out.println(decimal+digits[a]);
return digits[a];
}
decimal = decimal * base + horner(digits,base);
return decimal;
}
Hint: Create a helper method which takes an additional
indexparameter telling it what array index to look at. The helper should call itself recursively, incrementingindexeach time.Then the main function just has to kick off the process at the first index.
This isn't a full solution but it should be enough to get you going.