recursion

This is a discussion on recursion within the C++ Programming forums, part of the General Programming Boards category; aaggggg doing this with two variables is confusing. I can't figure out what to do next this is all I ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    recursion

    aaggggg doing this with two variables is confusing. I can't figure out what to do next this is all I have:
    Code:
        void powers(int x, int y)
        {
        if(y+1==0)
        return;
        powers(y-1)
        }
    Please explain how do do this.

    Edit: For negative powers how can I get it to ignore negative symbols.
    Last edited by cerin; 02-21-2005 at 01:36 PM.
    My computer is awesome.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What are you trying to do?

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I assume your function should recursively calculate x to the power of y?

    The recursive definition is:
    x^y = 1 [if y==0]
    x^y = x * x^(y-1) [otherwise]

    Turn this into code!

    Negative powers cannot be calculated by mere integers.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well a function that did that would return a value and I see a void return type

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    like this?
    Code:
        void powers(int x, int y)
        {
        if(y==0)
        cout<<"1";
        powers(x*x(y-1))
        }
    I plan to make two more functions. Two negatives=positive. One negative and one positive= negative. right?
    My computer is awesome.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Explain what you are trying to do first.

    Are you trying to display the result of x raised to y? Are you trying to calculate and return it? Dealing with negative powers is easy as x raise to -2 is just 1 divided by x raise to 2.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I'm trying to get the result from x to the y power,
    My computer is awesome.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok for ease lets say that the domain of x is all integers and the domain of y is all positive integers.

    Now we look at what x^y means. (For this post ^ means raised to the power of and not bitwise XOR).
    x^y means that x is multipled by itself y times. It can also be written as
    x * x^(y-1)
    Well x^(y-1) can be written as
    x * x^(y-2)
    Well x^(y-2) can be written as
    x * x^(y-3)
    etc etc until you have
    x * x^(y-y)

    This is the essence of your recursive function.

    The function header should be
    Code:
     int power (int x, unsigned y)
    The basic flow is
    Code:
    If y is equal to 0
      return 1
    Else
      return x times x^(y-1)
    Last edited by Thantos; 02-21-2005 at 05:49 PM. Reason: mistake in the return type

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Try this. Not coded to test for accuracy. Otain base and exponent. If exponent not an integer probably best to use built in pow() function which allows both base and exponent to be doubles. Else declare a variable of type double called result which is assigned return value of function called for the first time. The function itself should therefore return a double. Declare a variable called temp which is of type double within each function call. If y == 0 you're done with function calls. else if y > 0 then y -=1; and if y < 0 then y += 1 for each successive function call. If y > 0 then temp = x * func(x, y) and if y < 0 then temp = (1/x) * func(x, y) and if y == 0 then temp = 1. Return temp from each function call to unwind the recursive function calls.
    You're only born perfect.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Isn't that quite a bit like what I had?
    Is this right?
    Code:
        int powers(int x, unsigned y)
        {
        if(y==0)
        cout<<"1";//any base to the power of 0 is 1
        return 1;
        powers(x*x(y-1))
        }
    My computer is awesome.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I took out cout. It says x cannot be used as a function. How can I make it execute the powers function. If I put it in switch statement it has a syntax error.
    In function 'int powers(int x, unsigned int)'
    My computer is awesome.

  12. #12
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    return 1;
    This ends the execution of your function. Are you sure you want to put this before you call powers()?

    Code:
    powers(x*x(y-1))
    Do you mean to put

    Code:
    powers(x*x, (y-1))
    ?

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Is this any better?
    Code:
        int powers(int x, unsigned y)
        {
        if(y==0)
        return 1;
        else
        powers(x*x, (y-1))
        }
    Edited!! again

    What about my other questions?
    Last edited by cerin; 02-21-2005 at 06:46 PM.
    My computer is awesome.

  14. #14
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    No, you're still going to return 1 no matter what happens the first time you run the function. If you have multiple statements inside an if-statement, surround them with curly braces.

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    There is no need for the cout at all in this function. It calculates the value, it doesn't display it.

    Also you need to work on your indentation.

    Code:
    int powers(int x, unsigned y)
    {
      if(y==0)
        return 1;
      else
        return x * powers(x, y-1); /* x * x^(y-1) */
    }
    remember that if a function returns a value every branch needs to return a value

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 10:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21