Thread: Recursive function that changes an integer to binary

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    34

    Recursive function that changes an integer to binary

    I need to write a recursive function that takes an integer number (say 12) and change it to binary (1100) and I have no idea how to code it. Does anyone have any ideas?
    Also, I have write another recursive function that takes an interger, say 12 again, and will add the two digits so it is 3 (this function isn't as important). Thanks!
    Aaron

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    The function should take a parameter that tells it what bit it has to evaluate. The function would simply display this particular bit and dcrement that parameter by one before calling itself. The terminating condition would be this parameter equalling 0.

    So if you want to display a 32 bit int then the function call would look like:

    display (12, 32);

    That should get you started.
    Last edited by samGwilliam; 04-10-2005 at 05:33 PM.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    Ummm, ok, it sounds great; but I'm lost at how to do it.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Quote Originally Posted by advocation
    Ummm, ok, it sounds great; but I'm lost at how to do it.
    The first thing you need to do is develop an algorithm for converting decimal to binary. You can google that. After you have your algorithm, then you can look at implementation.

    edit: http://scholar.hw.ac.uk/site/computi...8.asp?outline= is a good explanation on how to do the conversion, and it leads right to a recursive implementation.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    I got it, cool; thanks!

    Oh yeah, any idea on how to do the other problem i have now?

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Quote Originally Posted by advocation
    I got it, cool; thanks!

    Oh yeah, any idea on how to do the other problem i have now?
    For the second part, it seems to me that you want to recursively divide by 10 to break the number down to its digits, then add them up. Your test to break the recursion will be when the remainder is 0.

  7. #7
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Think!

    Code:
    void display (int val, int x)
    {
         // Display bit x code goes here.
    
         display (val, x - 1);
    }
    But make sure you don't infinitely recur. That's all the direction you should need if you have the ability to freely think.
    Last edited by samGwilliam; 04-10-2005 at 08:39 PM.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM