Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, i\'m using eclipse to code. I\'m practicing on recursion and one of the c

ID: 3868725 • Letter: H

Question

Hello, i'm using eclipse to code. I'm practicing on recursion and one of the challenges I'm trying to accomplish is to do a change of base formula recursivly. Mostly, i am trying to verify log10(50) = 1.69897 as an example. However, according to my JUnit test, my actual result was not the same as 1.69897. As i debugged my rescursion method, it stops at the base case. Below, I will paste the recursion code but I also if you can break down the math to see how recursive method works for the sake of my benefit. Thank you!

public double changeOfBase(int number, int base)

   {

       int quotient = base / number;

       int remainder = base % number;

  

       //Base case

       if(quotient == 0)

       {

           return remainder;  

       }

      

       return changeOfBase(quotient, number) + remainder;

   }

Explanation / Answer

Bro, lets first have a look into your code and understand what your code does...

public double changeOfBase(int number, int base) {
        int quotient = base / number;
        int remainder = base % number;
   
        //Base case
        if(quotient == 0)
        {
            return remainder;    // <<- what it returns is remainder which is an integer
        }
       
        return changeOfBase(quotient, number) + remainder; // <<- it returns somthing plus remainder where remainder is an Integer
                                          //^----- even the recursive call will end up with a base class returning an Integer
                                                                 //^---------And Integer plus an Integer is an Integer
    }

So as you go through the comments you will realize that your code returns only integer....

But what does it return??

let us pass 12 and 2 to your function

changeOfBase (12, 2) = changeOfBase(6,2) + 0 //good enough? not the base case
                                 =(changeOfBase(3,2) + 0) + 0
                                 = ((changeOfBase(1,2) + 1) + 0) + 0
                                 = ((1 + 1) + 0) + 0 = 2

Looks like it sums up the digits of binary equivalent of 12 i.e. (1100)