Thread: Recursive Trace

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    17

    Recursive Trace

    I really need help learning how to get the output of this program.. I'm not completely lost but I need it in the simplest terms. I have a test soon and I'm trying to study.

    Code:
    /*Recursive Trace
       Written by Ron
       April 2006
    */
    
    #include <stdio.h>
    
    
    int barph(int a[5], int upchuck);
    
    
    int main(void)
    {int a[5]={11, 13, 15, 17, 19};
    int krud;
    krud=0;
    krud=krud*barph(a,3);
    printf("final result is %d\n", krud);
    return 0;
    }
    
    
    int barph(int a[5], int upchuck)
    {int sick;
    sick=1;
    if (upchuck>1)
            sick=a[upchuck]+barph(a,upchuck-1)+3;
    printf("upchuck is %d, sick is %d\n", upchuck, sick);
    return(sick);
    }
    It prints out:

    upchuck is 1, sick is 1
    upchuck is 2, sick is 19
    upchuck is 3, sick is 39
    final result is 0

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Have you sat down with a piece of paper and traced out the values of the variables? If so, where did you get stuck?

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    17
    I get stuck where on the sick=a[upchuck]+barph(a,upchuck-1)+3;
    because I don't understand how to figure out the value of barph and get the answer 19.


  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    You know what the values of a and upchuck-1 are at this point, so go through the function (again!) with those numbers.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    17
    I'm not so sure I really do though.. is "a" 17 and is "upchuck-1" 2? What do I do with those numbers multiply them? How does that get 19? I guess that's where I'm lost.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    You call the function again. So you start back up at line 22, but now upchuck is 2, since that was the value of the second parameter

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    17
    I got that part. How do I put these numbers together to get 19?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Kim Valentin View Post
    I got that part. How do I put these numbers together to get 19?
    You don't.

    You need to keep going. Go back to line 22 and start again. When you get to line 26 the second time, you will have another function call, and you will go back (again) to line 22 and start (yet) again. When you see the function call "barph( , )" you have to do that function call to figure out what that value is -- it's not an immediate thing.

  9. #9
    Registered User
    Join Date
    Oct 2013
    Posts
    17
    I'm still lost as ever but thanks for trying.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Put a printf() at the start of barph as well.
    Print something useful, like the parameter values.
    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. what this function does..(i tried to trace it)
    By transgalactic2 in forum C Programming
    Replies: 23
    Last Post: 10-29-2008, 10:01 AM
  2. g++ trace
    By barneygumble742 in forum Linux Programming
    Replies: 1
    Last Post: 08-11-2006, 04:23 AM
  3. how to trace C in linux
    By afreedboy in forum Linux Programming
    Replies: 4
    Last Post: 06-25-2004, 01:00 PM
  4. TRACE for MSVC?
    By aker_y3k in forum C++ Programming
    Replies: 1
    Last Post: 09-02-2002, 11:25 AM
  5. TRace files
    By Nutshell in forum C Programming
    Replies: 3
    Last Post: 04-16-2002, 12:56 PM

Tags for this Thread