Need help w/ program assignment cmpsc121


Write a program that requests a factorial position from the user and calculates it using recursion
This is not a homework site. We won't do your homework for you. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again. As it is impossible to find derivative of function without knowledge in arithmetic, you cannot do more complex tasks in programming without clear understanding of basics
For recursion, you'll need two parts. A stop condition, and the calling of the function inside itself, just decremented.

So if you were trying to do some kind of addition recursion, you'd say
1
2
3
4
5
6
7
int add( int x )
{
    if( x == 0 )
        return 0;
    else
        return x + add( x-1 );
}


This way is actually called tail-end recursion. More efficient (in my opinion) than head-end recursion, for the simple reason of it will check the stop condition first, ensuring a little more safety especially when you're dealing with recursion involving memory or something.

Note that a stop condition always will involve if variable == something, return a value.


Now all you have to do is remember what the definition of a factorial is, and apply the same concept there.
Topic archived. No new replies allowed.