-1

I have to write a program converting from any base to decimal using recursion and Horner's scheme. All I can use is:

  1. 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.
  2. 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;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Msitake
  • 13
  • 2
  • Hey, sorry, I didn't mean to sound like this. I basically have nothing I could share because I have no idea how to implement a counter so that I could create a terminating base for the recursion. So I have this but I know it makes no sense, have been trying to do this since Friday.. – Msitake Jan 17 '21 at 09:00

1 Answers1

0

Hint: Create a helper method which takes an additional index parameter telling it what array index to look at. The helper should call itself recursively, incrementing index each time.

public static int hornerHelper(int[] digits, int index, int base) {
    // majority of code here
    // recursively call hornerHelper(digits, index + 1, base) at some point
}

Then the main function just has to kick off the process at the first index.

public static int horner(int[] digits, int base) {
    return hornerHelper(digits, 0, base);  // start at index 0
}

This isn't a full solution but it should be enough to get you going.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578