Thread: Exam Question Help!

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    8

    Exam Question Help!

    This question came up in an exam I had.
    The answer is 120 can someone explain to me how this is?

    Q.
    What is the output of this program?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int func(int x);
    
    
    int main()
    {
        int x = 5;
        x = func(x);
        printf("%d \n", x);
    }
    
    
    int func(int x)
    {
        if(x > 1) return x*func(x-1);
        return 1;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Trace through the recursion. Write down what happens on paper, if you need to. You will find that it is a recursive implementation of a mathematical construct that you have probably seen before.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Get a pencil and paper, and mentally go through the program line by line, using the pencil and paper to keep track of the value of 'x' wherever it changes.

    This might be a bit more difficult to represent on paper, since it uses a recursive function, but figuring it out how to visualize it will help your understanding.

    [edit] Too slow.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    One might exclaim that a certain operation relates 5 and 120.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The best way to trace a program is either on paper (in your head for more experienced programmers for this program) or using a debugger.

    If you do NOT know how to use a debugger; I would suggest varying the input. In this program the value of x is the input value.

    Code:
    int x = 5;
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Another way - to use the unit test. Another way - to use the unit test. This is similar to fitness classes. Start doing it in his youth, when no health problems. It is better than start when the health is undermined.

    Test-driven development - Wikipedia, the free encyclopedia

    Test driven development in C

    C Testing for Embedded Applications with greatest

    Code:
    #include "greatest.h"
    
    
    int func(int x)
    {
        if(x > 1) return x*func(x-1);
        return 1;
    }
    
    
    TEST func_1()
    {
        ASSERT_EQ(func(1), 1);
        PASS();
    }
    
    
    TEST func_2()
    {
        ASSERT_EQ(func(2), 2);
        PASS();
    }
    
    
    TEST func_3()
    {
        ASSERT_EQ(func(3), 6);
        PASS();
    }
    
    
    TEST func_4()
    {
        ASSERT_EQ(func(4), 24);
        PASS();
    }
    
    
    TEST func_5()
    {
        ASSERT_EQ(func(5), 120);
        PASS();
    }
    
    
    SUITE(the_suite)
    {
        RUN_TEST(func_1);
        RUN_TEST(func_2);
        RUN_TEST(func_3);
        RUN_TEST(func_4);
        RUN_TEST(func_5);
    }
    
    
    GREATEST_MAIN_DEFS();
    
    
    int main(int argc, char **argv) {
        GREATEST_MAIN_BEGIN();      /* command-line arguments, initialization. */
        RUN_SUITE(the_suite);
        GREATEST_MAIN_END();        /* display results */
    }
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zub
    Another way - to use the unit test. Another way - to use the unit test. This is similar to fitness classes. Start doing it in his youth, when no health problems. It is better than start when the health is undermined.
    When the requirements are known, writing a unit test is a great idea. In this case, the reader does not know what the code is supposed to do, only what the code actually does. Writing a unit test does not work because the test parameters are unknown. Reading a unit test to help understand the code will work, but that's not the scenario here.

    Good idea for development, but not "another way" for understanding code with unknown requirements and no existing unit tests. Once one understands the intention of the code, then unit tests could be written.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with question from exam in c...
    By engti in forum C Programming
    Replies: 9
    Last Post: 02-11-2012, 07:42 AM
  2. Exam question help: Take 2
    By ÉireKarl in forum C Programming
    Replies: 11
    Last Post: 05-02-2010, 02:12 PM
  3. Exam question help
    By ÉireKarl in forum C Programming
    Replies: 116
    Last Post: 05-02-2010, 12:03 PM
  4. another exam question
    By rjeff1804 in forum C Programming
    Replies: 4
    Last Post: 02-12-2003, 10:29 PM
  5. exam question
    By teja22 in forum C Programming
    Replies: 2
    Last Post: 10-08-2001, 08:03 PM

Tags for this Thread