HELP ME ON RECURSION PLEASE 
Author Message
 HELP ME ON RECURSION PLEASE

I get the Idea of the thing "well sort of) Like if you were to input 6 the
way to get the answer would be 6*5*4*3*2 =720

so the factorial would be 720

But what I dont get is how the following code ends up to be 81 after
I've put down this function

int three_powered(int power)
{
    if  ( power <1)
    return (1);
else
    return (3 * three_powered(power - 1));

Quote:
}

ok here a equals 4

   {

      printf("\n to the power of %d is %d" a, three_powered(a));
   }

Here is where a turns into 81 but How did it do it I know how I called the
function and all but I don't understand all the math that went on?

Also I would like to know is there a great deal of importance of learning
this and should I spend extra time learning this?

Thanks,
Jason Marroquin



Sat, 13 Jan 2001 03:00:00 GMT  
 HELP ME ON RECURSION PLEASE

Quote:

> int three_powered(int power)
> {
>     if  ( power <1)
>     return (1);
> else
>     return (3 * three_powered(power - 1));
> }

> ok here a equals 4
> Here is where a turns into 81 but How did it do it I know how I called the
> function and all but I don't understand all the math that went on?

three_powered() is called taking 4.  Before it can return, it reaches a
call to three_powered(), taking 3. (And so on down to 0, which returns
one, thanks to the if statement.)  Since three powered is multiplied by
three each time,

three_powered(4) == 3 * three_powered(4 - 1);
three_powered(3) == 3 * three_powered(3 - 1);
three_powered(2) == 3 * three_powered(2 - 1);
three_powered(1) == 3 * three_powered(1 - 1);
three_powered(0) == 1; /* remember the if? this is what it's for! */

so now we have three_powered(4) == 3 * (3 * (3 *(3 * (1))))

since 3 * 3 * 3 * 3 * 1 = 81, that's what three_powered(4) returns.
There, now that wasn't so hard, was it? :-)

This works because fuctions can call any other function (which is in
scope!), including themselves.  Is recursion useful?  Don't ask that
here, you'll start another argument nobody will win.

Technically functions don't equal something, they return it.  It was
much easier to explain this way, so this is what I did. :-)

This is one of the more confusing parts of TYC in 21 days (I recognized
the example).  Also watch out for the linked list section later on.  It
seems that this book has two authors.  Peter Aitken, who writes the good
parts, and Bradley Jones, who writes the bad parts.  (I actually rewrote
the code in the linked list section after one reading and improved it
greatly.  That's how bad it was!)

--

(supporter of the campaign against grumpiness in c.l.c)
"Microsoft Patents Ones, Zeros" --headline in the Onion



Sat, 13 Jan 2001 03:00:00 GMT  
 HELP ME ON RECURSION PLEASE

Quote:

> I get the Idea of the thing "well sort of) Like if you were to input 6 the
> way to get the answer would be 6*5*4*3*2 =720

> so the factorial would be 720

> But what I dont get is how the following code ends up to be 81 after
> I've put down this function

> int three_powered(int power)
> {
>     if  ( power <1)

it should be:

if (power < 2)

or

if (power <= 1)

Quote:
>     return (1);
> else
>     return (3 * three_powered(power - 1));
> }

it should be:

return (power * three_powered(power - 1));

Quote:
> ok here a equals 4

>    {

>       printf("\n to the power of %d is %d" a, three_powered(a));
>    }

> Here is where a turns into 81 but How did it do it I know how I called the
> function and all but I don't understand all the math that went on?

> Also I would like to know is there a great deal of importance of learning
> this and should I spend extra time learning this?

> Thanks,
> Jason Marroquin

Below I paste a code that works.

Francisco.

#include <stdio.h>

int a, b;

int three_powered (int);

int main()
{
 printf ("Enter an integer number: ");
 scanf ("%d", &a);

 b = three_powered (a);

 printf("The factorial of %d is %d\n", a, b);

 return (0);

Quote:
}

int three_powered(int power)
{
    if  ( power < 2)
    return (1);
else
    return (power * three_powered(power - 1));
Quote:
}



Sun, 14 Jan 2001 03:00:00 GMT  
 HELP ME ON RECURSION PLEASE

--
Increase the Peace!
Charles LaCour

Quote:


>> I get the Idea of the thing "well sort of) Like if you were to input 6
the
>> way to get the answer would be 6*5*4*3*2 =720

>> so the factorial would be 720

>> But what I dont get is how the following code ends up to be 81 after
>> I've put down this function

>> int three_powered(int power)
>> {
>>     if  ( power <1)

>it should be:

>if (power < 2)

>or

>if (power <= 1)

>>     return (1);
>> else
>>     return (3 * three_powered(power - 1));
>> }

>it should be:

>return (power * three_powered(power - 1));

>> ok here a equals 4

>>    {

>>       printf("\n to the power of %d is %d" a, three_powered(a));
>>    }

>> Here is where a turns into 81 but How did it do it I know how I called
the
>> function and all but I don't understand all the math that went on?

>> Also I would like to know is there a great deal of importance of learning
>> this and should I spend extra time learning this?

>> Thanks,
>> Jason Marroquin

>Below I paste a code that works.

>Francisco.

>#include <stdio.h>

>int a, b;

>int three_powered (int);

>int main()
>{
> printf ("Enter an integer number: ");
> scanf ("%d", &a);

> b = three_powered (a);

> printf("The factorial of %d is %d\n", a, b);

> return (0);
>}

>int three_powered(int power)
>{
>    if  ( power < 2)
>    return (1);
>else
>    return (power * three_powered(power - 1));
>}

I'm sure this code works great.  The problem is that it doesn't do what the
author wants it to do.  He isn't seeking the factorial, he's seeking to
essentially multiply 3 by the number of times given in the variable 'power'.
In other words, 3 squared, 3 cubed, 3 to the 4th power, etc...


Sun, 14 Jan 2001 03:00:00 GMT  
 HELP ME ON RECURSION PLEASE

Quote:


> > I get the Idea of the thing "well sort of) Like if you were to input 6 the
> > way to get the answer would be 6*5*4*3*2 =720

> > so the factorial would be 720

> > But what I dont get is how the following code ends up to be 81 after
> > I've put down this function

> > int three_powered(int power)
> > {
> >     if  ( power <1)

> it should be:

> if (power < 2)

> or

> if (power <= 1)

No it should not; 3 to the power of 0 = 1

Quote:

> >     return (1);
> > else
> >     return (3 * three_powered(power - 1));
> > }

> it should be:

> return (power * three_powered(power - 1));

Depends on what the function is supposed to do. As it is, it returns 3 to
the power of its argument (hence its name).

[snip]

Quote:
> Below I paste a code that works.

> Francisco.

[snip]

I think there is a confusion of two functions - factorial and three_powered.

Arjan Bokx
--
(replace underscore with dash for email reply)
(vervang underscore door minstreepje voor antwoord per epost)



Sun, 14 Jan 2001 03:00:00 GMT  
 HELP ME ON RECURSION PLEASE


...

Quote:
>> > int three_powered(int power)
>> > {
>> >     if  ( power <1)

>> it should be:

>> if (power < 2)

>> or

>> if (power <= 1)

>No it should not; 3 to the power of 0 = 1

More to the point 3 to the power of 1 is 3, not 1.

Quote:

>> >     return (1);

The parentheses here while harmless are unnecessary.

--
-----------------------------------------


-----------------------------------------



Sun, 14 Jan 2001 03:00:00 GMT  
 HELP ME ON RECURSION PLEASE
[problem statement]

Quote:
> But what I dont get is how the following code ends up to be 81 after
> I've put down this function

[invocation]
printf("3 to the power of %d is %d\n", 4, three_powered( 4));

[source]

Quote:
> int three_powered(int power)
> {
>     if  ( power <1)
>     return (1);
> else
>     return (3 * three_powered(power - 1));
> }

> Here is where a turns into 81 but How did it do it I know how I called the
> function and all but I don't understand all the math that went on?

        You called three_powered( 4) so right now three_powered is running
with power = 4, let's see what's going on under the hood:

        1.  three_powered is called, power = 4.
        2.  the condition (power < 1) is tested.  4 < 1 is false, so the
            else is executed.
        3.  return a value of 3 * three_powered( power - 1).

FLAG 1          WAIT.  We can't return yet, before we can return that
                value we must call three_powered( 3) (4 - 1).  So let's
                do that, but we will come back to this point later.

        4.  three_powered is called, power = 3.
        5.  the condition (power < 1) is tested.  3 < 1 is false, so the
            else is executed.
        6.  return a value of 3 * three_powered( power - 1).

FLAG 2          WAIT.  We can't return yet, before we can return that
                value we must call three_powered( 2) (3 - 1).  So let's
                do that, but we will come back to this point later.

        7.  three_powered is called, power = 2.
        8.  the condition (power < 1) is tested.  2 < 1 is false, so the
            else is executed.
        9.  return a value of 3 * three_powered( power - 1).

FLAG 3          WAIT.  We can't return yet, before we can return that
                value we must call three_powered( 1) (2 - 1).  So let's
                do that, but we will come back to this point later.

        10. three_powered is called, power = 1.
        11. the condition (power < 1) is tested.  1 < 1 is false, so the
            else is executed.
        12. return a value of 3 * three_powered( power - 1).

FLAG 4          WAIT.  We can't return yet, before we can return that
                value we must call three_powered( 0) (1 - 1).  So let's
                do that, but we will come back to this point later.

        13. three_powered is called, power = 0.
        14. the condition (power < 1) is tested.  0 < 1 is true, so the
            following statement is executed.  (this is called the
            recursion's termination condition, all recursions must
            define such a condition.  the purpose of the recursion
            should be to get closer and closer to this termination
            condition.)
        15. return a value of 1.  (when terminating, notice that the
            recursion no longer returns a value defined in terms of
            itself, but a specified value or calculation in known terms.)

        16. we are back at FLAG 4.  (this is called unwinding the recursion.)
        17. return a value of 3 * 1 (3).  (completing step 12.)

        18. we are back at FLAG 3.
        19. return a value of 3 * 3 (9).  (completing step 9.)

        20. we are back at FLAG 2.
        21. return a value of 3 * 9 (27).   (completing step 6.)

        22. we are back at FLAG 1.
        23. return a value of 3 * 27 (81).    (completing step 3.)

        This final return value is the result of the original function
invocation, three_powered( 4).

Quote:
> Also I would like to know is there a great deal of importance of learning
> this and should I spend extra time learning this?

        Best to study the answer I gave to your question right above..
figure out how I described everything, learn how to describe the flow of
a program yourself.  Analyzing programs is a very useful technique, spend
extra time learning it (as a fringe benefit, recursion and everything else
will be easier to observe.)  ;)

        There isn't a great deal of importance in learning recursion unless
you are working with certain abstractions and algorithms which are more
readily expressed in a recursive fashion.  If you want to learn Lisp, you
must know recursion (the question of why learn Lisp is a mystery wrapped
in an enigma which I won't get into.)

                                                        Derek Harmon



Sun, 14 Jan 2001 03:00:00 GMT  
 HELP ME ON RECURSION PLEASE

Quote:


> > int three_powered(int power)
> > {
> >     if  ( power <1)
> it should be:

> if (power < 2)
> or
> if (power <= 1)

        Why?  Then the function wouldn't work right.

Quote:
> >     return (3 * three_powered(power - 1));
> it should be:

> return (power * three_powered(power - 1));

        3 is correct.  Hence the name of the function, three_powered,
it raises 3 to the power given as a parameter.  You appear to be
rewritting it to do factorials but that wasn't the original intent
(or at least, I'd definately change the function name).  ;)

                                                        Derek Harmon



Sun, 14 Jan 2001 03:00:00 GMT  
 HELP ME ON RECURSION PLEASE

Quote:

>I get the Idea of the thing "well sort of) Like if you were to input 6 the
>way to get the answer would be 6*5*4*3*2 =720

>so the factorial would be 720

>But what I dont get is how the following code ends up to be 81 after
>I've put down this function

>int three_powered(int power)
>{
>    if  ( power <1)
>    return (1);
>else
>    return (3 * three_powered(power - 1));
>}

>ok here a equals 4

>   {

>      printf("\n to the power of %d is %d" a, three_powered(a));
>   }

>Here is where a turns into 81 but How did it do it I know how I called the
>function and all but I don't understand all the math that went on?

>Also I would like to know is there a great deal of importance of learning
>this and should I spend extra time learning this?

>Thanks,
>Jason Marroquin

    Lets see what the computer does :

     three_powered(4)=
     3*three_powered(3)=
     3*3*three_powered(2)=
     3*3*3*three_powered(1)=
     3*3*3*3*thre_powered(0)=3*3*3*3*1=81

 You did 3 in power 4.

   Remember important thing : Use recusrion only if necessary
 (like Fibonachi or Factorial).

  write this function :

   int three_powered(int power)
{
    return (power * power * power);

Quote:
}

   Or if you want to use recursion :

int three_powered(int power, int a)
{
     if (power<1) return 1; else
        return (a*three_powered(power-1,a);

Quote:
}

    Another important thing : use longint as the type of the function
because int is until 32767 so you will get normal results till 31
32 in power 3 is 32768

   If you have any questions you are invited to call me by clicking
on my IPN (below)
___________________________________________________________________________
Alexander Zlotnik
WebTelecom Integration Executive
Private IPN: http://qms.WebTele.com:8080/WebTelecom/call_request?alex

http://www.WebTele.com  
Put a real salesperson within your virtual store!
___________________________________________________________________________



Tue, 16 Jan 2001 03:00:00 GMT  
 HELP ME ON RECURSION PLEASE

Quote:


[snip]

> >int three_powered(int power)
> >{
> >    if  ( power <1)
> >    return (1);
> >else
> >    return (3 * three_powered(power - 1));
> >}

[snip]

>  You did 3 in power 4.

>    Remember important thing : Use recusrion only if necessary
>  (like Fibonachi or Factorial).

Recursion is not necessary for calculating Fibonacci numbers or factorials.

Quote:
>   write this function :

>    int three_powered(int power)
> {
>     return (power * power * power);
> }

This calculates something different: the parameter to the power three.

Quote:
>    Or if you want to use recursion :

> int three_powered(int power, int a)
> {
>      if (power<1) return 1; else
>         return (a*three_powered(power-1,a);
> }

And here the 'three' in the function name has become totally misplaced -
this calculates 'a' to the power 'power' - again a different function with
the same name.
It is misleading to give these functions the same name since they give
different results.

Quote:
>     Another important thing : use longint as the type of the function
> because int is until 32767 so you will get normal results till 31
> 32 in power 3 is 32768

That depends on what range you need.
Also, the standard requires int to span -32767 to 32767, but compilers are
free to provide a more generous range.

Arjan Bokx
--
(replace underscore with dash for email reply)
(vervang underscore door minstreepje voor antwoord per epost)



Tue, 16 Jan 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. help please; recursion

2. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

3. Please help!!!!Please help!!!!Please help!!!!

4. help: Guru needed please please please

5. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

6. PLEASE PLEASE HELP HELP...question on interleaving C functions

7. Help on recursion

8. Fibonacci recursion using dynamic stacks (HELP)

9. novice needs help with recursion

10. URGENT HELP WITH TREES and recursion

11. HELP with balancing braces program using recursion.

12. Recursion help!

 

 
Powered by phpBB® Forum Software