Thread: Sample Test Question/Answer Explanation

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    11

    Sample Test Question/Answer Explanation

    Ok, ive been given a sample test and this is really confusing me could someone explain how this works?

    Many Thanks

    Jeev

    Code:
    Q9
    What is the signature of the following function definition?
    int orange( int counter )
    
    {
    if ( counter > 5 )
    {
    counter = counter – 1;
    return ( orange( counter – 1 ) + counter );
    }
    else
    {
    return 2;
    }
    }
    
    A void orange( int );
    B int orange( int );
    C void orange( counter );
    D int orange( void );
    
    Q10
    
    Given the function definition in Question 9, what is the output of the following C program?
    int main( void )
    {
    printf( “%d”, orange( 9 ) );
    }
    
    
    
    
    A 8
    B 12
    C 16
    D 20

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    I'll answer the best I can, and I may be wrong as terminology is a slippery thing. I will not give you the answers, but instead try to explain what they're asking about.

    Q9: The "Signature" describes the function, made up of its name, number and types of parameters, and return type. The contents of the function body do not take part in determining the signature, so can be ignored in answering the question. Look at the function declaration or function prototype to determine the signature. (C++ uses singatures, C usually doesn't allow function name overloading, thus doesn't concern itself with signatures.)

    Q10: This should be easy -- simply pretend that you're the computer and calculate the result. It's not difficult. The function is recursive, but with the value given (9), it's only a few steps and you've got an answer. If you can't run through this on paper (or in your head), you still have a lot to learn about programming and computer problem solving.
    Insert obnoxious but pithy remark here

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    Quote Originally Posted by filker0
    I'll answer the best I can, and I may be wrong as terminology is a slippery thing. I will not give you the answers, but instead try to explain what they're asking about.

    Q9: The "Signature" describes the function, made up of its name, number and types of parameters, and return type. The contents of the function body do not take part in determining the signature, so can be ignored in answering the question. Look at the function declaration or function prototype to determine the signature. (C++ uses singatures, C usually doesn't allow function name overloading, thus doesn't concern itself with signatures.)

    Q10: This should be easy -- simply pretend that you're the computer and calculate the result. It's not difficult. The function is recursive, but with the value given (9), it's only a few steps and you've got an answer. If you can't run through this on paper (or in your head), you still have a lot to learn about programming and computer problem solving.

    I realise i have alot to learn the answer is 16. Thing is i do not understand how the recursive function gets this result?

    Jeev

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    Would i be right in thinking the recursive function keeps looping from the value 9 until the value = 2!

    there for 8,7,6,5,4,3,2 < it takes 7 steps for it to do this 9+7 = 16

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Q9: The "Signature" describes the function, made up of its name, number and types of parameters, and return type. The contents of the function body do not take part in determining the signature, so can be ignored in answering the question. Look at the function declaration or function prototype to determine the signature. (C++ uses singatures, C usually doesn't allow function name overloading, thus doesn't concern itself with signatures.)
    If you know what a function prototype is, that might help you, too.
    Code:
    printf( “%d”, orange( 9 ) );
    Careful with the quotes. With most compilers that won't compile.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by jeev2005
    I realise i have alot to learn the answer is 16. Thing is i do not understand how the recursive function gets this result?
    Code:
    int orange( int counter )
    {
        if ( counter > 5 )
        {
            counter = counter – 1;
            return ( orange( counter – 1 ) + counter );
        }
        else
        {
             return 2;
        }
    }
    1. call orange with counter == 9
    2. o1: 9 (the value of counter) > 5
    3. o1: counter <- 8 (the old value of counter - 1)
    4. o1: call orange with a value of 7 (counter - 1, or the original value of counter - 2)
    5. o2: enter function orange with counter == 7
    6. o2: 7 > 5
    7. o2: counter <- 6
    8. o2: call orange with the parameter value 5
    9. o3: enter function orange with counter == 5
    10. o3: 5 <= 5 ("5 > 5" is false)
    11. o3: return 2
    12. o2: value of call to function orange == 2
    13. o2: return to caller with a value of 6 + 2 (current local counter + the result of orange(5)), aka 8
    14. o1: value of call to function orange == 8
    15. o1: return to caller with a value of 8 + 8 (current local counter + the result of orange(7)), aka 16
    16. value of call to function orange == 16
    Remember that the parameter variable "counter" in the function is local to that invocation of that function. Its value is not shared with other invocations of the function.

    I hope this helps.
    Insert obnoxious but pithy remark here

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Would i be right in thinking the recursive function keeps looping from the value 9 until the value = 2!

    there for 8,7,6,5,4,3,2 < it takes 7 steps for it to do this 9+7 = 16
    It keeps looping until it reaches 5, at which point it returns 2. The result is 2 + 6 + 7 + 8 + ....
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Actually, it's 2 + 6 + 8 + ... for odd numbers, and 2 + 5 + 7 + ... for even numbers.
    Insert obnoxious but pithy remark here

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're right, it is. I missed the first decrement of counter.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by dwks
    You're right, it is. I missed the first decrement of counter.
    That's okay... It's easy to miss. I believe that the instructor is trying to test understanding of recursion and scoping of variables.
    Insert obnoxious but pithy remark here

  11. #11
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Well this is how I see it:

    We call orange(9)
    if (9 > 5)
    counter=9-1 // now counter is 8
    return orange(8-1) + 8;

    So now we know that the result is 8 + some number. We do the same thing over again:

    We call orange (7)
    if (7 > 5)
    counter=7-1 // now counter is 6
    return orange(6-1) + 6;

    So now we know that the result of the second call is 6 plus some number. We do it again:

    We call orange(5)
    if (5 > 5)

    else
    return 2;

    So nothing to do here except return 2, and in the place where orange(6-1) was at we can replace the result of this call (because it returns int), so now we have:

    return 2 + 6;

    and now we're back to the original call, and we can replace orange(8-1) with 6+2 so:

    return 6+2 + 8;

    and that's the value that gets returned from the original call. So hopefully you can see that it's just a matter of substituting the values into the variables.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sustring test puzzle
    By WDT in forum C# Programming
    Replies: 3
    Last Post: 06-29-2009, 07:19 AM
  2. Linked Lists
    By Bleu_Cheese in forum C++ Programming
    Replies: 13
    Last Post: 12-21-2007, 09:17 PM
  3. Problems with an addition operator
    By jamjar in forum C++ Programming
    Replies: 12
    Last Post: 03-28-2003, 01:47 PM
  4. test scores
    By ucme_me in forum C Programming
    Replies: 4
    Last Post: 11-14-2001, 01:48 PM