Recursive function, trying to understand the logic.

This is a discussion on Recursive function, trying to understand the logic. within the C Programming forums, part of the General Programming Boards category; I have been given this function and I am trying to understand the logic behind it. I was given: puzzle(25) ...

  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,049
    Depends on what puzzle() is supposed to be doin'.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    Out of curiosity, but are you familiar with binary and bit shifting?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,638
    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,049
    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,806
    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, 09: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-30-2005, 12:21 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21