Thread: Tracing this code, sum1 help please ! been stuck for damn long !!!! :(

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Tracing this code, sum1 help please ! been stuck for damn long !!!! :(

    Code:
    #include <stdio.h>
     int count = 0;
     void f(int x);
     int main()
     {  
         f(2);
         printf("%d",count);
         return 0;
     }
     void f(int x)
     {
         
         count++;
         if (x > 0)
         {
             f(x/2);
            
             f(x/2);
           
         }
     }
    could someone please help me trace this code through ?? ive been trying for the whole nite but still cant get the proper trace ... thanks !!!!

  2. #2
    Registered User DevoAjit's Avatar
    Join Date
    Jun 2011
    Location
    Ludhiana, Punjab, India, India
    Posts
    32
    what u want to do with these codes... in user function you used f(x/2) two times and why u used int count before main block... u shud use count in main block... your programm will work well..

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Hi, thanks for the reply.. this is 1 of my exam questions, its exactly how it appears in the paper, im kind of stuck how to trace through this program, the final answer is 7.. but i dun know how count increases from 0 to 7...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So add at line 12
    printf("x=%d, count=%d\n", x, count );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-28-2011, 05:27 PM
  2. Help Tracing through code
    By NICKD54 in forum C Programming
    Replies: 15
    Last Post: 06-19-2011, 09:03 AM
  3. what is the best method of tracing this code on paper..
    By transgalactic2 in forum C Programming
    Replies: 19
    Last Post: 04-10-2009, 12:46 PM
  4. what is the best way of tracing code on paper
    By transgalactic2 in forum C Programming
    Replies: 8
    Last Post: 03-29-2009, 05:06 PM
  5. pls sum1 make this programme
    By mprinyal in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2006, 11:34 PM