# Thread: Brand Newbie needs help with Factorial

1. ## 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. > 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. 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. 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. 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. Code:
```int factorial(int num)
{
return num>0 ? num*factorial(num-1) : 1;
}```
This is a recursive solution.

(Edit: added semicolon)

7. 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. You can use a for loop to solve the factorial.

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

9. zuki_r: ??

10. 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. 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.

12. Thanks Alot!

Popular pages Recent additions