Tips in tracing C Codes

This is a discussion on Tips in tracing C Codes within the C Programming forums, part of the General Programming Boards category; I really suck at tracing in exams but I'm good when it comes to the programming part. Can anyone here ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    California
    Posts
    3

    Unhappy Tips in tracing C Codes

    I really suck at tracing in exams but I'm good when it comes to the programming part. Can anyone here teach me how to trace a code fast and efficiently. Everytime I trace someones code I always got confused especially if it is full of recursion and pointers.

    Example: showing the output of this code. 1 page full of codes. (no computer of course it's an exam so I'll do it manually)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It may help to try to be as literal as possible -- don't try to look at it and "read" the whole thing -- just look one line at a time, and do what it says to do. Definitely be neat as to what the current state of all the variables is. When a recursive call happens, be sure to make a new set of variables, and be sure to return to the right place in the code when it's done. After it's all done, then you can try to look at the big picture -- see what happened as the program progressed. Edit: I should add, that this will (should?) improve with practice, and I would be really interested to hear how you can effectively debug if you can't "read" your code as it is, and not how you intended it to be.

    Maybe an example. First just find the output of the following code; then look at what happened as the program ran and tell me what it does.
    Code:
    int mystery(int *a, int x);
    
    int main(void) {
        int array1[6] = {19, 25, 34, 6, 11, 0};
        int array2[10] = {7, 5, 2, 9, 6, 1, 4, 3, 8, 0};
        printf("%d\n", mystery(array1, 99));
        printf("%d\n", mystery(array2, 10));
        return 0;
    }
    
    int mystery(int *a, int x) {
        int temp;
        if (*a == 0)
            return x;
        if (*a < x)
            temp = *a;
        else
            temp = x;
        return mystery(a+1, temp);
    }
    Last edited by tabstop; 11-06-2008 at 11:51 PM.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Location
    California
    Posts
    3
    Maybe I really need to practice more and improve my logic and memory. Ok thanks for the help.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah, I would have answered 11 and 8 to this question.

    [edit: that was without using pen and paper tho]
    Last edited by MK27; 11-08-2008 at 10:31 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Did you do better with pen and paper?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Did you do better with pen and paper?
    As soon as I started writing down the series 19-99, 25-19, 34-19 I could see what was happening, but of course by then I had run the program and know what the answer is supposed to be.

    I guess that makes me guilty of looking at it all at once and presuming I understood the entirity of what I was looking at.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming Tips
    By webmaster in forum General Discussions
    Replies: 18
    Last Post: 12-20-2009, 02:14 PM
  2. codes
    By IfYouSaySo in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 11-18-2005, 03:09 PM
  3. C++ Programming Tips
    By gflores in forum C++ Programming
    Replies: 20
    Last Post: 09-14-2004, 08:53 PM
  4. action replay codes
    By c++.prog.newbie in forum Game Programming
    Replies: 2
    Last Post: 02-28-2004, 08:47 AM

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