Tracing through a recursive program in C

This is a discussion on Tracing through a recursive program in C within the C Programming forums, part of the General Programming Boards category; Say we have the program: Code: #include <stdio.h> void binary_representation (int n) { if (n != 0) { binary_representation(n/2); printf("%d", ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    15

    Tracing through a recursive program in C

    Say we have the program:
    Code:
    #include <stdio.h>
    
    void binary_representation (int n)
     {
       if (n != 0)
      {
       binary_representation(n/2);
       printf("%d", n%2);
      }
     }
    
    int main (void)
     {
      binary_representation(13);
      printf("\n");
      return 0;
    }

    The problem asks for the output of the program
    So my process was like:
    1) the main function calls binary_representation and sents 13

    2) binary_representation(13) passes the conditional if-statement and calls binary_representation(13/2) while printing out 13%2 = 1 in the process

    3) binary_representation(13/2) passes the condition statement and calls binary representation(6/2) while printing out (13)%2 = 1 in the process

    4) binary_representation (6/2) passes the condition statement and calls binary representation (3/2) while printing out (6/2)%2=0 in the process

    5) binary representation (3/2) passes the condition statement and calls binary representation (1/2) while printing out (3/2)%2=1 in the process

    6) condition fails, exits the function, main prints a newline and returns zero

    Is my solution be a correct interpretation to this program?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Make sure you keep your order straight -- "calls binary representation(13/2) while printing out 13%2=1 in the process" isn't possible. It does one thing, then it does the other.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No.

    The recursive calls will all be made, just like you think - but NO printing will be done at all throughout those recursive calls, since the printf() line of code, is done AFTER the recursive call:

    Call to binary_r() //push onto the stack
    Call to binary_r() "
    Call to binary_r() "
    Call to binary_r() "
    Return and print //pop off the stack
    Return and print "
    Return and print "
    Return and print "

    All the printing is done on successive pops off the stack

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    Thanks alot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Help, simple program, unsure newbie :(
    By Aslan14 in forum C Programming
    Replies: 13
    Last Post: 11-14-2010, 05:07 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  3. Help!!this program is Recursive Function or not?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-25-2002, 04:40 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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