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

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I am thinking the problem is too big for you to solve.

    I would try to break the problem down into two separate recursive functions.
    One that converts the input value of input base into base 10.
    Two converts the base 10 value into a char array.

    Then, the third function calls the two recursive functions.

    NOTE: I think Grumpy was hinting that this is not the best method; but, it is easier for me to understand so, likely easier for you to understand.
    I think he implied you did NOT need to convert to base 10 when going from input base to output base value.

    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

  2. #17
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Please explain your logic on the recursive step?

    Code:
    return num + baseConversion( num%newBase, num/base, newBase );
    Why do you think the 2rd parameter value should be changing?
    Why do you think the 1st parameter should be num%newBase ?

    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

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    My quick 15 minute test program.
    Note: My program only does base 2 to 10; but, I think this just means the code in fromBaseTen into a function that returns a char array. This would likely take another 15 minutes to half for me to do. But, I have no idea if you know malloc or static char arrays.
    I used this code in my baseConversion function; but, it will likely need moved to the new fromBaseTen function.

    Code:
    char* baseConversion(int num, int base, int newBase)
    {
        static char result_string[MAX_RESULT_LENGTH+1] = {0};
        
        snprintf(result_string, MAX_RESULT_LENGTH, "%d", fromBaseTen( toBaseTen( num, base ), newBase ));
        
        return result_string;
    }
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Max number of charators in the result of baseConversion */
    #define MAX_RESULT_LENGTH 32
    
    char* baseConversion(int num, int base, int newBase);
    int toBaseTen( int num, int base );
    int fromBaseTen( int num, int base );
    
    int main()
    {
        printf("Testing!\n");
        
        printf("toBaseTen(0,8) := %d\n", toBaseTen(0,8));
        
        printf("fromBaseTen(0,8) := %d\n", fromBaseTen(0,8));
        
        printf("toBaseTen(10,8) := %d\n", toBaseTen(10,8));
        
        printf("fromBaseTen(8,8) := %d\n", fromBaseTen(8,8));
        
        printf("toBaseTen(17,8) := %d\n", toBaseTen(17,8));
        
        printf("fromBaseTen(15,8) := %d\n", fromBaseTen(15,8));
        
        printf("toBaseTen(127,8) := %d\n", toBaseTen(127,8));
        
        printf("fromBaseTen(87,8) := %d\n", fromBaseTen(87,8));
        
        printf("baseConversion(127, 8, 10) := %s\n", baseConversion(127, 8, 10));
        
        printf("baseConversion(127, 8, 2) := %s\n", baseConversion(127, 8, 2));
        
        return 0;
    }
    Code:
    Testing!
    toBaseTen(0,8) := 0
    fromBaseTen(0,8) := 0
    toBaseTen(10,8) := 8
    fromBaseTen(8,8) := 10
    toBaseTen(17,8) := 15
    fromBaseTen(15,8) := 17
    toBaseTen(127,8) := 87
    fromBaseTen(87,8) := 127
    baseConversion(127, 8, 10) := 87
    baseConversion(127, 8, 2) := 1010111
    Tim S.

    FYI: I am NOT posting the code that takes real thought because the only way to learn it is to figure it out yourself.
    Last edited by stahta01; 10-21-2014 at 07:21 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

  4. #19
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    4 chars
    Last edited by Mike Garber; 10-21-2014 at 07:29 PM. Reason: ........

  5. #20
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    I made it!!! Sorry for the double post. Thank you for your help too!, can you help me understand the converting numbers to strings like you do in your baseConversion function?
    I literally wrote down on paper what was happening and what my number literally must equate to in a recursive call.
    Code:
    int baseTen( int num, int base )
    {
        if( num < 10 )
            {
            return num;
            }
        return num + baseTen( num/10%10*base , base*base );
    }
    Last edited by Mike Garber; 10-22-2014 at 02:30 PM.

  6. #21
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The method I used will not apply to you; I would use a char array set equal to "0123456789ABCDEF" and then use a index of it to set the value in a allocated char array.

    NOTE: your "return num + baseTen( num/10%10*base , base*base );" has little resemblance to my code.

    Note: Your method might work; but, mine is much simpler than yours.

    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. #22
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    Quote Originally Posted by stahta01 View Post
    The method I used will not apply to you; I would use a char array set equal to "0123456789ABCDEF" and then use a index of it to set the value in a allocated char array.

    NOTE: your "return num + baseTen( num/10%10*base , base*base );" has little resemblance to my code.

    Note: Your method might work; but, mine is much simpler than yours.

    Tim S.
    Actually, It doesn't work and I dont know why. Im doing the operations to make my number I am so frusterated right now and its due on Friday Im so screwed

  8. #23
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    toBaseTen(127,8) := 87
    ((((1)*8) + (2)) * 8) + (7) = 87

    Maybe re-looking at the math will give you a hint.

    Edit: added extra unneeded () to maybe give better hint.

    Tim S.
    Last edited by stahta01; 10-23-2014 at 12:12 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. #24
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    Made any progress on this? I've been told it becomes more straight forward by creating functions like iToStr and strToI.

  10. #25
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    So, input values coming in at base 10 are rather easy to convert to other bases. Please correct me if I'm wrong but to convert a number of base 10 to another base, you must divide by the output base until the quotient is zero. You must calculate the remainder of dividing each quotient

    Code:
    baseConversion(int input, int base, int outBase) {
    
    
    if ( input != 0 ) {
    if ( base == 10 ) {
    baseConversion(input/outBase, base, outBase); return input%outBase;
    } if ( outBase == 10 ) { // Not sure what to do for this case } else { // Not sure what to do for this case }
    }
    }


    However, that's about as far as I've made it. I'm confused about what to do for other bases.

  11. #26
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by jcconnell View Post
    Code:
    if ( input != 0 ) {
    Code:
    baseConversion(input/outBase, base, outBase);
    I think these two steps are closer than the OP; but, still NOT right, yet.

    Your recursion step likely only works for input 0 thru 9.

    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

  12. #27
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    I believe otherwise actually. This was designed to work only for inputs in base 10. Pulling another example from the internet helped me visualize it.

    input = 93, base = 10, outBase = 8.

    93 % 8 = 5, 93 / 8 = 11
    11 % 8 = 3, 11 / 8 = 1
    1 % 8 = 1, 1 / 8 = 0

    93base 10 = 135 base 8.


    Now, I don't know what to do if base is another number. Are you guys suggesting we look into use the ASCII tables?

  13. #28
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by jcconnell View Post
    Are you guys suggesting we look into use the ASCII tables?
    No, I am NOT.

    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. #29
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Hint: Many (I think likely all) recursive function has one or more recursive steps.

    One form of recursive step is

    return <Value1> <OP1> <Value2> <OP2> recursive_function_name(Parameter1<OP3> <Value3>, ...);

    Where <OP1>, <OP2>, and <OP3> are operations like +,-,/,*,%, and etc.
    Where Value1, Value2, or Value3 are constants or input parameters.
    Where Parameter1 is the first parameter passed to the recursive function.

    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

  15. #30
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    For converting to base 10:

    return str[0] * pow(baseOut, strlen) + baseConversion(str[++], baseIn, baseOut);

    Right direction?


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