Thread: Recursion function to change the base of a number and return the result

  1. #1
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59

    Recursion function to change the base of a number and return the result

    I am getting stuck on this hw problem/function because I need to do it recursively:

    baseConversion: takes three integer parameters and returns a string representing the answer of the base conversion. For example when baseConversion (6,10,2) is called, it will take 6 in base 10, convert it to base 2 and return the string “110”

    I am pretty sure I would have to convert the number into base 10 first and I think before I can do any of that, I need to turn my first number passed into a string because I need to multiply each 10s digit by a power of 10 and then add the results together. How might I go about doing this?

    All I have right now is the basic skeletal structure of the function:
    Code:
    char** baseConversion(int num, int base, int newBase){
        
    }
    I am not sure how I can access the different tens places.. For instance, lets say I have a number in base 3 I was to convert but the number is 2222. My number converted to base 10 is 2*3^3 + 2*3^2 + 2*3^1 + 2*3^0

    But how can I get to each 10s spot individually to do that arithmetic in a recursive function?
    Last edited by Mike Garber; 10-20-2014 at 02:19 PM. Reason: messed up base calc

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Why would you need to access the different "tens places"? That's a hint, BTW.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    I know this isnt working yet, but am I on the right track?
    Code:
    char** baseConversion(int num, int base, int newBase){
        
        if ( base != 10 )
        {
            int newNum=0;
            while ( num >= 1 )
            {
                int x = 0;
                num = num/base;
                newNum *=base;
            }
            
            
            
        }
         
    }
    Last edited by Mike Garber; 10-21-2014 at 10:48 AM. Reason: im so lost right now

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Likely no; but, I am NOT positive what grumpy suggestion meant.

    But, you have no recursion and you have a loop, instead; this is likely wrong.

    I suggest starting small with case zero, one, two, three input base 10 to output base 2.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You intend to use recursion, so there should not be a while loop.

    One problem I see is that your requirements are weird: the number to convert is given as an int. Therefore, the initial base does not make sense. For example, what should the result of baseConversion(10, 8, 10) be? According to your requirements, it means that 10 should be interpreted as being the representation of a number in base 8, then you convert the representation to base 10. Problem is, the value of the first argument is 10_10 (i.e., 10 in base 10), not 8_10 (which is what it would be if we are talking about 10_8). I suggest that you clarify this with your instructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Also, ask the instructor the range of base values allowed; normal is 2 to 16.
    But, I have seen 2 to 32, instead.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    Quote Originally Posted by stahta01 View Post
    Also, ask the instructor the range of base values allowed; normal is 2 to 16.
    But, I have seen 2 to 32, instead.

    Tim S.
    "The second parameter (the base of the original number) will always be in the range 2-10 inclusive. The third parameter (the base for the conversion) will
    be in the range 2-16 inclusive."

    ~Teacher

    My problem about this is how can I do a recursive call with integer values to return a string?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I already gave you may suggestion/hint do input base 10, value 0, output base 2.

    If you can NOT do the above then, I can NOT help you without giving you the answer.

    Edit: If you post your attempts at solving my suggest or the real problem; you will likely get help.
    Posting tries that seem to be unrelated to the real problem will NOT likely get help.

    Hint: The value of zero would be what I would try to use as a stopping condition.

    Tim S.
    Last edited by stahta01; 10-21-2014 at 12:17 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If this is too hard
    I already gave you may suggestion/hint do input base 10, value 0, output base 2.
    Try doing input base 10, value 0, output base 10.
    Then input base 10, value 10, output base 10.

    Before trying output in base 2.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    Code:
    int baseConversion( int num, int base, int newBase ){
    
    
        if( base <= 0 )
            return num;
        return num + baseConversion( num%newBase, num/base, newBase );
    }
    This works for inputs 10, 10, and 10. I tried inputs 5, 10, and 2... My output is 6 when I call the function in a print statement. Im trying the best I can here.. Im not even ready to make this function return a string if I cant even get the right value to put into the string.
    Last edited by Mike Garber; 10-21-2014 at 01:26 PM.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do you know what a recursion function is?
    Because, I am having my doubts that you know how to write one.
    Please explain to me what your stopping condition is for your recursion function?

    Note: The stopping condition is what stops the recursion from being infinite; some people use the term base condition(s) instead for a nearly the same meaning.

    Why are you dividing by base? It might be right it might be wrong.
    You never answered the other person (laserlight) question; the answer to it would say if this is right or wrong.
    Code:
    return num + baseConversion( num%newBase, num/base, newBase );
    Tim S.
    Last edited by stahta01; 10-21-2014 at 01:36 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    A recursive function is one that calls on itself. The stopping condition in my case is when the base hits 0. Im just learning the concepts of recursion so Im having a hard time knowing what my stop condition should be and my recursive function call.

    Lets say I have the number 10 in base 10 and I want it in base 2. This means I should divide num by the new base and keep the remainder until my num/newbase = 0. So then should I return this instead?
    Code:
    return num + baseConversion( num%newBase, num/newBase, newBase );
    *I am not sure what laserlights question is asking so I dont know how to answer it.
    Last edited by Mike Garber; 10-21-2014 at 01:46 PM.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Does the input value of integer x = 10; mean 8 or 10 in base ten when the input base is 8?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  14. #14
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    Quote Originally Posted by stahta01 View Post
    Does the input value of integer x = 10; mean 8 or 10 in base ten when the input base is 8?

    Tim S.
    If the input is 10 and the base is 8, the base 10 number would be 8

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    So dividing by the base is likely wrong in my opinion; more likely dividing by 10 is needed in the recursion step.

    Quote Originally Posted by Mike Garber View Post
    If the input is 10 and the base is 8, the base 10 number would be 8
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't output a decimal number from a function result
    By samwillc in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2013, 01:59 PM
  2. change and return arrays from a function
    By cuizy in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 03:52 PM
  3. Calculate log base e by recursion function.
    By alice in forum C Programming
    Replies: 0
    Last Post: 04-23-2004, 11:51 PM
  4. How to change number data from base 256 to base 16?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 12:19 AM
  5. how to change and return a string from function?
    By cywong18 in forum C Programming
    Replies: 8
    Last Post: 03-23-2002, 08:24 AM

Tags for this Thread