Thread: Brand Newbie needs help with Factorial

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    6

    Unhappy Brand Newbie needs help with Factorial

    Okay here goes..I just started my first class of C++ and my teacher is impossible to understand. I have had two years of VB and realize that VB and C++ are very different. I am an anxious learner but after my first class my teacher has given us some assignments and explained nothing. He has told us that our textbook won't do us much good so I am feeling at a loss. I don't want anyone to do my work for me, but I could really use some good instructional help to get me rolling. I'm sure I will be asking several questions in my first few weeks of class. Again I wouldn't expect anyone to do my work for me..just some help. So here is my first assigned problem.

    1. Write an interative function power1 to compute xn for n>0.

    This is the problem as it is listed in the book. Now I am not sure of how to solve it. I was given a formula to supposedly
    use to reach my objective.

    Formula: factorial(n)=n * (n-1) * (n-2) * ...*1 for any integer n>0


    Could someone help shed some light on this for me. This newbie would be greatly appreciative. Thanks to all who do (Y)

    Nick

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > 1. Write an interative function power1 to compute xn for n>0.

    Are you sure you're supposed to get a factorial function, or a power function out of this?

    If you show what you've got so far, I'll help.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    6
    Well from what I know I believe it is supposed to be a factorial function. Does that formula look like a familiar formula. Hope thats not a stupid question. Does power1 mean that the n in xn is =1. Then it would be x1? So in the formula that I listed above, n would be = to 1 throughout the formula. Does any of that make sense. Give me time and I won't be stuttering around this so much. Thanks alot by the way!

    Nick

    ps. the teach is an strange duck and no one in the class is clued in to him yet.

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Well, the formula you listed is a factorial function, but the name "power1" threw me for a loop.

    Anyways - get a book out (what book are you using as your text, btw?) and look up looping - that's what you'll want for this.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    6
    Is this recursion? Cause the chapter I found some information on this in was the chapter on recursion. Here is the text:

    http://www.amazon.com/exec/obidos/AS...035032-5884059

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Code:
    int factorial(int num)
    {
     return num>0 ? num*factorial(num-1) : 1;
    }
    This is a recursive solution.

    (Edit: added semicolon)
    Last edited by Sang-drax; 09-04-2002 at 04:13 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    6
    Does this look like how you would solve for f(n)?

    1[ (1-1) * (1-2) * 1]

    1[ ( 0 * -1) * 1]

    1[0 * 1]

    1 * 0 = 0

    n=0


    ???

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    4
    You can use a for loop to solve the factorial.

    ex.. for (i=n;i>0;i--) when n>0

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    zuki_r: ??
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    6
    Okay guys I think i'm on the right track. I just got out of class and explained what you guys have shown me and he actually said I was ahead of what he was wanting. For this first assignment he is looking for more of psuedocode type information. List out how, in logic terms, how to find use recursion to find the factorial. He is not to explanitory in his lectures so he leaves alot to be questioned. For now I have written out what he is looking for. We are going to be getting into loops next week so i'm sure I will have some questions. Thanks everyone!

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    A more exotic way (and not that useful) of calculating factorials recursively:

    Code:
    template<int n> int factorial()
    {
        return n*factorial<n-1>();
    }
    
    int factorial<1>()
    {
        return 1;
    }
    
    //Use the function like this:
    cout << "5! = " << factorial<5>();
    The advantage is that the factorial is calculated compile-time, the disadvantage is that n must be a constant.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    6

    Thumbs up

    Thanks Alot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  2. Factorial
    By foxman in forum Contests Board
    Replies: 27
    Last Post: 07-11-2008, 06:59 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Basic Factorial from 1-10
    By AaA in forum C Programming
    Replies: 20
    Last Post: 05-28-2005, 07:39 AM
  5. factorial output
    By maloy in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 03:28 PM