Help Tracing through code

This is a discussion on Help Tracing through code within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm currently in a Summer class dealing with C Programming and I'd like some help tracing through this ...

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

    Help Tracing through code

    Hey guys, I'm currently in a Summer class dealing with C Programming and I'd like some help tracing through this code so I understand how it works exactly. Any help would be greatly appreciated!

    Code:
    #include <stdio.h>
    void flipper(int,char *);
    int main(void){
    
    char path[5] ;
    path[4]=0;
    
    flipper(0,path);
    
    system("pause");
    return 0;
    }
    
    void flipper(int level, char* path){
    *(path+level)='A'; /* coin flip side A */
    if(level==3) /* flipped coin 4 report results */
    printf("%s\n",path);
    else
    flipper(level+1,path); /* flip next coin */
    *(path+level)='B'; /* coin flip side 'B' */
    if(level==3) /* flipped four coins if you want to change the number of coin tosses shange the 3 to something else both here and above*/
    printf("%s\n",path); /* report the path */
    else
    flipper(level+1,path); /* not done flip next coin */
    
    return;
    }

    This is supposed to give the possible permutations for flipping a coin. I'm a bit confused on a few things done here.

    Like... what exactly the char path[5]; is, path[4] i believe would be to say there are 4 flips. Then I'm also confused about the * (path+level) part and the if statements. Thanks again!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Step 1... indent the code properly. You'll be surprised how much easier it is to read and follow...

    Step 2... BE the computer... just do what each line tells you to do, keeping track of the values of the variables as you go.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    My apologies on the indention, I think the biggest issue I'm having right now is just being unsure of what char path[5] ; is doing. I just looked back through and I understand what's going on with the level variable.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by NICKD54 View Post
    My apologies on the indention, I think the biggest issue I'm having right now is just being unsure of what char path[5] ; is doing. I just looked back through and I understand what's going on with the level variable.
    That's an array declaration.... 5 characters stored in the variable named path.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    To understand that code you need to have an understanding of the following

    1. Recursive functions
    2. Pointer
    3. Pointer Arithmetic
    4. Arrays

    Do you know what they are? Do you have understanding of any of them? If not, it probably too early to start looking into this. I would strongly recommend getting your foundation right.

    ssharish
    Last edited by ssharish2005; 06-16-2011 at 06:07 PM.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    I do understand these things for the most part ssharish, it's been a couple semesters since my last C class so I'm still getting back into the hang of syntax and everything. I believe I was mixing up the array declaration and the next line.

    Why would that array be declared to 5? And then the next line, path[4] = 0; sets the fifth spot in that array equal to 0 correct?

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    path[4] = 0
    This code could have been

    Code:
    path[4] = '\0';
    This is to specify the NULL char to terminate the string.
    And that should answer your question.

    ssharish
    Last edited by ssharish2005; 06-16-2011 at 06:21 PM. Reason: To reflect tabstop comment
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays in C start at an index of 0 and go through SIZE-1. Thus:
    Code:
    type array[ SIZE ];
    Provides elements:
    Code:
    array[ 0 ]
    ...
    array[ SIZE-1 ]
    You cannot access array[ SIZE ] because that is out of bounds.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ssharish2005 View Post
    Code:
    path[4] = 0
    This code could have been

    Code:
    path[4] = '\0';
    This is to specify the NULL char to terminate the string. Clearly there is no necessity to declare and array of 5 char's. 4 char array length would have done the job, with no issues clearly. It’s just poor design.

    And that should answer your question.

    ssharish
    If a four-character array had been declared, how would you be able to access path[4]?

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Agreed I totally misunderstood that :-/

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    I tried playing with the char path[5] ; and I get the same output as long as the number is greater than or equal to the number in the line below it ( path[4]=0; )

    So now tracing through this recursion, I see how level is incremented from 0 to 3 and I believe the *(path+level) = 'A'; assigns A to the spot in the array which would initially be 0+0, then 0+1, until level = 3, then it prints the string. Then path[3] becomes B and it prints again, but then we have the return statement and I'm not sure what happens after that.

    Please let me know if I sound like I'm on the right track and what the next step here is.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *(path+level) is the same as path[ level ]


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    thank you for pointing that out to me quzah, I figured it meant something like that at least

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Could anyone help me with understanding what happens after the first two calls? I'm a little lost there.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Proper indentation goes a long way.
    Code:
    void flipper(int level, char* path){
        *(path+level)='A'; /* coin flip side A */
        if(level==3) /* flipped coin 4 report results */
            printf("%s\n",path);
        else
            flipper(level+1,path); /* flip next coin */
        *(path+level)='B'; /* coin flip side 'B' */
        if(level==3) /* flipped four coins if you want to change the number of coin tosses shange the 3 to something else both here and above*/
            printf("%s\n",path); /* report the path */
        else
            flipper(level+1,path); /* not done flip next coin */
    
        return;
    }
    Write it out on paper:
    Code:
    f(0)
        here + 0 = A
        f(1)
            here + 1 = A
                f(2)
                    here + 2 = A
                    f( 3 )
                        here + 3 = A
                        print string
                        here + 3 = B
                        print string
                     < return
                    here + 2 = B
                    f( 3 )
                        here + 3 = A
                        print string
                        here + 3 = B
                        print string
                    here + 2 = B
                    < return
    I will leave the rest as an exercise to the reader.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tracing pointers
    By ~Kyo~ in forum Game Programming
    Replies: 5
    Last Post: 04-29-2010, 06:45 PM
  2. what is the best method of tracing this code on paper..
    By transgalactic2 in forum C Programming
    Replies: 19
    Last Post: 04-10-2009, 01:46 PM
  3. what is the best way of tracing code on paper
    By transgalactic2 in forum C Programming
    Replies: 8
    Last Post: 03-29-2009, 06:06 PM
  4. Tracing in Unix
    By suwie in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2004, 08:02 PM
  5. tracing
    By cman in forum C Programming
    Replies: 4
    Last Post: 02-08-2003, 02:35 PM

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