Thread: Functions,factorials asap help needed! :(

  1. #1
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50

    Unhappy Functions,factorials asap help needed! :(

    Hi there!
    I have to make c porgram, that finds factorial of number. Using defined function find and print factorials for whole numbers from 1 to n, up to the maximum possible integer value.
    Hope for some help!

    Here's my try, but i dont know how ti make it with my own fuctions:
    Code:
     #include<stdio.h>
    int main(){
      long f=1;
      int i,num,min=0,max;
    
    
    
      printf("Type n: ");
      scanf("%d",&max);
    
      printf("Factorial 1-n: ");
      for(num=min;num<=max;num++){
        f=1;
    
        for(i=1;i<=num;i++)
          f=f*i;
    
        printf("%ld ",f);
      }
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357

    you already have your own defined function

    Just rename main() ...

    Code:
    int my_defined_function(){
        /* YOUR EXACT CODE AS ABOVE */
    }
    and add a different main that calls your function
    Code:
    int main(void) {
        my_defined_function();
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is an incidental concern that the factorial of 13 will overflow a 32 bit integral type.
    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.

  4. #4
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50

    whats wrong now?

    Quote Originally Posted by grumpy View Post
    There is an incidental concern that the factorial of 13 will overflow a 32 bit integral type.
    Code:
    #include<stdio.h>
    int main(){funkcija();
      long f=1;
      int i,num,min=0,max;
      int funkcija (){
    
    
      printf("Type n: ");
      scanf("%d",&max);
    
      printf("Factorial 1-n: ");
      for(num=min;num<=max;num++){
        f=1;
    
        for(i=1;i<=num;i++)
          f=f*i;
    
        printf("%ld ",f);
      }
    
      return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > whats wrong now?
    Study qny's post again.

    You've nested the definition of funkcija INSIDE main - this is wrong.
    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.

  6. #6
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by Salem View Post
    > whats wrong now?
    Study qny's post again.

    You've nested the definition of funkcija INSIDE main - this is wrong.
    It shows errors Can somebody put this together for me?

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Solarwin View Post
    It shows errors Can somebody put this together for me?
    That would rob you of the learning experience. Did you try to do as Salem suggested? If so, show your updated code.

    It seems you're having trouble with the basics of functions. Perhaps a brief read-up is in order. Here is one possible source: Functions in C - Cprogramming.com

  8. #8
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by Matticus View Post
    That would rob you of the learning experience. Did you try to do as Salem suggested? If so, show your updated code.

    It seems you're having trouble with the basics of functions. Perhaps a brief read-up is in order. Here is one possible source: Functions in C - Cprogramming.com
    Yup, i tried. This is what i did:
    Ok thanks for the link.
    Code:
    #include<stdio.h>
    int funkcija(){
    int main()
      long f=1;
      int i,num,min=0,max;
      int funkcija (){
    
    
      printf("Type n: ");
      scanf("%d",&max);
    
      printf("Factorial 1-n: ");
      for(num=min;num<=max;num++){
        f=1;
    
        for(i=1;i<=num;i++)
          f=f*i;
    
        printf("%ld ",f);
      }
    
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome - read through it well. Take your time and make sure you understand the concepts. Note that "main()" is also a function. Let us know if you need clarification on anything.

  10. #10
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Thank you all for your fast replies. But that code i entered before still have some errors:/

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome ... but fast replies from you indicate that you're not taking our advice, not reading up on why your program has errors, and not putting in a whole lot of effort.

    I'll spare you the trouble of clicking the link again and scrolling down a few lines to see the proper way to implement a function.

    Code:
    #include <stdio.h>
    
    /* function declaration */
    return-type function_1(data-type variable);
    
    int main(void)
    {
        // ...
    
        function_1(x);  /* function call (actually using the function) */
    
        return 0;
    }
    
    /* function definition */
    return-type function_1(data-type variable)
    {
        // ...
    }

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For what it's worth, given the original problem I think the idea is to write a function that will compute a factorial, not to write a function that implements the whole program. Something like this, perhaps...
    Code:
    long factorial(int n);
    Still, what you're doing now is good practice and you may want to finish that first.
    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.

  13. #13
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by dwks View Post
    For what it's worth, given the original problem I think the idea is to write a function that will compute a factorial, not to write a function that implements the whole program. Something like this, perhaps...
    Code:
    long factorial(int n);
    Still, what you're doing now is good practice and you may want to finish that first.
    This is my homework that i have to send in few hours!:/Could u send me email with your code? i will do practise anyway

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Sorry but no, we don't directly do homework on this forum. Anyway I was just guessing based on the problem type, you weren't actually clear enough in the original post to figure out which interpretation is required by the assignment.

    Here are some thoughts. A function is a separate piece of code that you can run/invoke/call by its name. For this to work, you need a function prototype (name of function with arguments and a semicolon), which declares the function and lets you call it after that point. You need an actual function definition, which has the code within the function that will run when you call it. And then you need the function call itself, which is the point (e.g. in main) that you want to jump to the function's code and run. When the function is done, it will return to the original calling location. Example:
    Code:
    void two(void);
    
    int main() {
        printf("1\n");
        two();
        printf("3\n");
        return 0;
    }
    
    void two(void) {
        printf("2\n");
    }
    That prints 1, 2, 3. There are two other major things you can do with functions. The first is that you can pass in arguments,
    Code:
    void print(int x);
    
    int main() {
        printf("1\n");
        print(2);
        printf("3\n");
        return 0;
    }
    
    void print(int x) {
        printf("%d\n", x);
    }
    and the second is that you can return values.
    Code:
    int important(void);
    
    int main() {
        int i = important();
        printf("the function thinks this is important: %d\n", i);
        return 0;
    }
    
    int important(void) {
        return 42;
    }
    Of course, the two can be combined together for something like
    Code:
    long factorial(int n);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Needed asap
    By 2 legs of man in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2006, 01:29 PM
  2. Help Needed Please... Asap
    By Shabba in forum C Programming
    Replies: 2
    Last Post: 01-13-2004, 04:24 PM
  3. Help needed with a Project. ASAP Thanks
    By Shahram_z in forum C Programming
    Replies: 2
    Last Post: 10-31-2002, 03:12 PM
  4. Factorials and recursive functions
    By SadGuy in forum C Programming
    Replies: 8
    Last Post: 09-16-2002, 03:35 AM

Tags for this Thread