Thread: Recursion binary to decimal - question.

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    122

    Recursion binary to decimal - question.

    Hi,
    I was asked to program a recursion (in C), which converts a binary to decimal.
    The below code works well, yet I am not sure it fully exploits the potential of recursion (in particular the first two lines):

    Code:
    int convert_bin_to_dec(int num)
    {
        iter++;
        if (iter != 1) count*=2;
        if (!(num / 10)) return (num * count);
        return ((num % 10) * count + convert_bin_to_dec(num /10));
    }
    Iter and count are global variables set to 0 and 1 respectively.
    I'd sincerely appreciate any feedback/suggestions.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by peripatein View Post
    Hi,
    Code:
    int convert_bin_to_dec(int num)
    {
        iter++;
        if (iter != 1) count*=2;
        if (!(num / 10)) return (num * count);
        return ((num % 10) * count + convert_bin_to_dec(num /10));
    }
    Where is the input of the binary number?
    What is the format of the binary number?
    Why are you using global variables?
    Was this use of global variables allowed by the Instructor?
    Most good Instructors considered unneeded global variables to be wrong.

    Edit: This might be a case where your convert_bin_to_dec calls a recursive helper function instead of being the recursive function.
    It depends on the assignment requirements.

    Tim S.
    Last edited by stahta01; 04-23-2013 at 09:17 AM.
    "...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. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Why are global variables considered wrong in this case? We weren't explicitly forbidden to make use of global variables. How would you advise approaching it otherwise? The input of the binary number is in main(). Would you need that to assist me? I'd gladly post it if needed.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Is there a real reason why you CAN NOT pass the value of iter and count to the recursive function?
    If no, then using Global Variables is wrong; note, I think it is still wrong if you have what you think is a real reason.

    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
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    You didn't really elaborate how it ought to be handled differently. Would you recommend using another function to yield the number of binary digits the input has?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A helper function would allow you to set up iter and count inside and not have them as globals.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    How about this?
    Code:
    int convert_bin_to_dec(int num)
    {
        if (!(num / 10)) return (num);
        return (num % 10 + convert_bin_to_dec(num / 10) * 2); 
    }
    Is this valid?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I do not think the code you pasted now accounts for place value:

    For example

    2^0 2^1 2^2 2^3 2^4
    1 0 1 0 1

    which is 21.

    It's a specific multiplication you need to perform in each place.

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    But it works perfectly! You may run it and see for yourself.

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    And with your input (viz. 10101) 21 was indeed the answer the program yielded.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by peripatein View Post
    How about this?
    Code:
    int convert_bin_to_dec(int num)
    {
        if (!(num / 10)) return (num);
        return (num % 10 + convert_bin_to_dec(num / 10) * 2); 
    }
    Is this valid?
    Looks good to me; but, you need to test it and verify it works for input values.

    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. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    It does! (As I have verified)

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    If you have no further remarks or comments to make, I would like to thank you sincerely for your kind assistance!

  14. #14
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    I'd appreciate your comments on the following recursion, confirming that the number is a binary number (defined to be a positive number of 0's and 1's). Note, I am not allowed to use arrays, strings, pointers. We were instructed to use recursion!

    Code:
    int is_valid_bin_num(int num)
    {
        if (num / 10 > 0 && num % 10 <= 1) return (is_valid_bin_num (num / 10));
        if (num == 1 || num == 0) return 1;
        return 0;
    }
    It feels slightly cumbersome to me.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just checking: are you sure that your functions are supposed to have an int parameter and an int return type?

    I ask because it is sensible to talk about say, converting the binary representation of an integer as a string into the decimal representation of the integer as a string, or converting the binary representation of an integer as a string into the int value, but converting from int to int does not make sense to me.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. syntax question binary to decimal (i think)
    By ear9mrn in forum C Programming
    Replies: 1
    Last Post: 01-09-2011, 08:59 AM
  2. binary to decimal
    By yigster in forum C Programming
    Replies: 2
    Last Post: 03-31-2009, 04:00 AM
  3. Converting Decimal to Binary Numbers using Recursion
    By jaisch in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2005, 10:33 PM
  4. binary to decimal
    By owi_just in forum Tech Board
    Replies: 2
    Last Post: 04-02-2005, 10:24 AM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM