Thread: What is wrong with this function program?Unable to get output.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    45

    What is wrong with this function program?Unable to get output.

    A function program to calculate the factorial value of an integer.
    Code:
    main()
    {
    int a,fact;
    printf("Enter an integer");
    scanf("%d",&a);
    fact=facto(a);
    printf("The Factorial of the integer is%d",fact);
    }
    facto(x)
    {
    int i=1;
    for(i=1;i<=x-1;i++)
    {
    x=x*i;
    }
    return(x);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Start by:
    • Indenting your code properly.
    • Including the relevant headers.
    • Declaring your functions before use.
    • Declaring your functions with the appropriate return types and parameter types.
    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
    After fixing up the code per laserlight's suggestion, try printing the values of 'x' and 'i' in your "for()" loop. This is the simplest form of troubleshooting, and should be well within your capabilities.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Quote Originally Posted by laserlight View Post
    Start by:
    • Indenting your code properly.
    • Including the relevant headers.
    • Declaring your functions before use.
    • Declaring your functions with the appropriate return types and parameter types.
    So should I declare the function like this?
    Code:
    int facto();
    Also what do you mean by declaring functions with the appropriate return types and parameter types?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sameertelkar
    So should I declare the function like this?
    No, declare it to be a prototype like this:
    Code:
    int facto(int x);
    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

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Quote Originally Posted by laserlight View Post
    No, declare it to be a prototype like this:
    Code:
    int facto(int x);
    Thanks but still ain't getting the output.
    eg. when I type 3,I get the factorial something like -1011......?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code? (Note that we have not yet started on your actual problem.)
    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

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I take it you didn't try my advice in post #3?

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Quote Originally Posted by laserlight View Post
    What is your current code? (Note that we have not yet started on your actual problem.)
    Code:
    main()
    {
    int a,fact;
    printf("Enter an integer");
    scanf("%d",&a);
    fact=facto(a);
    printf("The Factorial of the integer is%d",fact);
    }
    int facto(int x)
    {
    int i=1;
    for(i=1;i<=x-1;i++)
    {
    x=x*i;
    }
    return(x);
    }

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Quote Originally Posted by Matticus View Post
    I take it you didn't try my advice in post #3?
    Why should I print x and i?I just want to return the value of x into fact.If you mean to say use 'return' within 'for' then it will print every multiplication in the calling function but I just want the factorial.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, that is still poorly formatted and incomplete. This is what I had in mind:
    Code:
    #include <stdio.h>
    
    int facto(int x);
    
    int main(void)
    {
        int a, fact;
        printf("Enter an integer");
        scanf("%d", &a);
        fact = facto(a);
        printf("The Factorial of the integer is%d", fact);
    }
    
    int facto(int x)
    {
        int i = 1;
        for (i = 1; i <= x - 1; i++)
        {
            x = x * i;
        }
        return(x);
    }
    Notice the header that is included and the declaration of facto before it is used in main. Notice that main now has its return type explicitly stated. Since you omitted to explicitly return a value from main, I have done the same since it just means "return 0;" at the end of main in the 1999 edition of the C standard and later.

    Now, onto your problem: as Matticus suggested, you need to observe what is going on in the loop in facto. Use a debugger, print the variables, whatever. You need to see what is happening to understand what is going wrong and hence fix it.
    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

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Why should I print x and i?
    Because if the function isn't working as expected, then that might give you a giant clue as to why. It will show you the values for each iteration of the loop, and might show you something very interesting.

    I don't mean "put it in and keep it in," I just mean so you can get a sense of what's happening in your code.

    That's the beauty of programming - it's easy to add temporary code for troubleshooting purposes.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Matticus
    That's the beauty of programming - it's easy to add temporary code for troubleshooting purposes.
    It is also easy to forget and leave it in, hence it is usually better to use a debugger if one is available.
    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

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    @laseslight,still getting the same output with your code.........-10112
    @matticus-I printed the value of x and i within for but still getting the same output.
    Is there a problem with my Turbo C or something?

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by laserlight View Post
    It is also easy to forget and leave it in, hence it is usually better to use a debugger if one is available.
    Very true - however, my advice was gauged on the simplistic level of the given program, and the presumed lack of experience by the OP. Those who are just starting out might be easily intimidated by such tools. Based on that, and the actual problem at hand, I thought recommending the "printf()" method in this case had more value.

    But you're right - getting used to debuggers early in the game would be a very valuable asset.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program compiles, but wrong output
    By mysterymann in forum C Programming
    Replies: 17
    Last Post: 11-30-2011, 10:51 PM
  2. Replies: 13
    Last Post: 10-05-2011, 09:12 AM
  3. getting wrong output from simple program
    By d387420489 in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 06:21 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 11:56 AM