Thread: Recursive function, trying to understand the logic.

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    Recursive function, trying to understand the logic.

    I have been given this function and I am trying to understand the logic behind it. I was given: puzzle(25) and puzzle(38); and the following code:
    Code:
    void puzzle (int n)
    {
        if (n!=0)
        {
            puzzle (n/2);
            putchar ('0'+n%2);
        }
    }
    I added a print statement and my outputs are, 11, 13, 06, 012 & 125 for puzzle(25) and 11, 02, 04, 19, 119 and 038 for puzzle(38). I am assuming i did this function correctly by havint it printf the (int n) value.

    Can someone help me with this? I really appreciate it and thanks!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Depends on what puzzle() is supposed to be doin'.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Out of curiosity, but are you familiar with binary and bit shifting?
    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

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What about the logic don't you understand. Do you have the basics of recursive programming? Do you understand what n/2 and n%2 do (and the other uses for n%2)? Do you understand the putchar statement? Please be a little more specific.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by itCbitC View Post
    Depends on what puzzle() is supposed to be doin'.
    I believe that is what my question is. What is the function 'puzzle' doing. I am trying to understand the logic behind it. This is the code I did in order for it to print;
    Code:
    #include <stdio.h>
    
    void puzzle (int n);
    
    void puzzle (int n)
    {
        if (n!=0)
        {
            puzzle (n/2);
            putchar ('0'+n%2);
            printf ("%d\n", n);
        }
    }
    
    int main()
    {
        puzzle (38);
    }

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Run it on your system w/o the printf() and correlate its output to the binary number system.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Help with the logic? The function repeatedly calls itself, each time doing so passing half the value it was previously called with until the value passed equals 0. Once it reaches 0, the function begins to exit, printing values as it goes - either 0 or 1 depending on whether the argument passed in to that particular instance of the function was even or odd. What you are left with (in the original version, not your "modified" version) should be a sequence of 0's and 1's that represent the original argument expressed in binary form. 25 (decimal) is 11001 in binary, 38 (decimal) is 100110 in binary.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Thank you itCbitC and hk_mp5kpdw.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  3. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM