Results 1 to 10 of 10
  1. #1
    mflb94 is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default problem with recursive method

    My problem is with this exercise:
    Write a recursive method called power that takes a double x, and an integer n and that returns x^n. Hint: a recursive definition of this operation is x^n = x*x^(n-1). Also, remember that anything raised to the zeroeth power is 1. Optional challenge: you can make this method more efficient, when n is even, using x^n=(x^(n/2))^2.

    Now call me crazy but I just can't see why recursion is necessary or how it could be implemented into this, here is the code I wrote:
    Java Code:
    package edu.vtc.aav10260.cis2261;
    import java.util.Scanner;
    /**
     * @author andre
     *
     */
    public class Power {
    
    	/**
    	 * @param args
    	 * @return 
    	 */
    	public static double main(String[] args) {
    		Scanner kbd = new Scanner(System.in);
    		System.out.println("Enter the values: ");
    		System.out.print("x= ");
    		double x = kbd.nextDouble();
    		System.out.print("n= ");
    		int n = kbd.nextInt();
    		return Math.pow(x, n);
    	}
    	}
    It works, and returns x^n just as the exercise said, but it specified to use a recursive method so if any of you guys could give me a hand in how recursion would work with this problem it would be appreciated.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: problem with recursive method

    A recursive method calls itself, so if you need to calculate the power of a number, how do you think you can do so using a method that calls itself? Break the problem down....what is the definition of power a ^ b ? Use an example where a = 3, b = 4: 3 ^ 4 = 3 * 3 * 3 * 3. In other words, 3 times itself 4 times...so what could you put inside a method that calls itself to calculate a times itself b times?

  3. #3
    mflb94 is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: problem with recursive method

    bump

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: problem with recursive method

    Evidently you don't care for advice given, in fact completely ignored it, which doesn't help your chances of receiving help here or elsewhere. And neither does cross-posting.

    Cross posted at help with recursive method

  5. #5
    mflb94 is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: problem with recursive method

    i didnt really understand what you were saying

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: problem with recursive method

    Quote Originally Posted by mflb94 View Post
    i didnt really understand what you were saying
    Then ask a question about it rather than ignoring it, because ignoring is a 2-way street.

  7. #7
    mflb94 is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: problem with recursive method

    ok well my question is, will you please show me how you would write this code?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: problem with recursive method

    Quote Originally Posted by mflb94 View Post
    ok well my question is, will you please show me how you would write this code?
    That's not how it's done here since we don't do other people's homework. Surely you've studied your text or a tutorial on recursion. Surely you've trued to solve this first yourself. so what about recursion confuses you? What doesn't work in your code attempt?

  9. #9
    mflb94 is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: problem with recursive method

    i just don't see how recursion can be used in this problem as I said the code that I wrote did exactly what the exercise says to and doesn't need recursion

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: problem with recursive method

    Quote Originally Posted by mflb94 View Post
    i just don't see how recursion can be used in this problem as I said the code that I wrote did exactly what the exercise says to and doesn't need recursion
    Just because you can solve it non-recursively doesn't mean that there isn't a recursive solution. The key as has been mentioned before is to work it out on paper first. But before you do that, see how other recursive methods, like the one for calculating Fibonacci numbers works.

Similar Threads

  1. Recursive Method
    By emschorsch in forum New To Java
    Replies: 11
    Last Post: 04-18-2011, 06:01 AM
  2. recursive method
    By michail in forum New To Java
    Replies: 0
    Last Post: 01-31-2010, 01:50 PM
  3. recursive method problem
    By melody in forum New To Java
    Replies: 1
    Last Post: 10-29-2009, 07:15 AM
  4. Java Recursive method problem
    By kj2009 in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2009, 03:19 PM
  5. Recursive Method
    By bluegreen7hi in forum New To Java
    Replies: 5
    Last Post: 11-29-2007, 04:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •