Results 1 to 10 of 10
Thread: problem with recursive method
 09272012, 04:17 PM #1Member
 Join Date
 Sep 2012
 Posts
 7
 Rep Power
 0
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^(n1). 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); } }
 09272012, 06:27 PM #2Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,641
 Rep Power
 9
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?
 09282012, 01:24 AM #3Member
 Join Date
 Sep 2012
 Posts
 7
 Rep Power
 0
Re: problem with recursive method
bump
 09282012, 01:33 AM #4Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,641
 Rep Power
 9
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 crossposting.
Cross posted at help with recursive method
 09282012, 01:37 AM #5Member
 Join Date
 Sep 2012
 Posts
 7
 Rep Power
 0
Re: problem with recursive method
i didnt really understand what you were saying
 09282012, 03:17 AM #6Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,641
 Rep Power
 9
 09282012, 03:47 AM #7Member
 Join Date
 Sep 2012
 Posts
 7
 Rep Power
 0
Re: problem with recursive method
ok well my question is, will you please show me how you would write this code?

Re: problem with recursive method
 09282012, 03:53 AM #9Member
 Join Date
 Sep 2012
 Posts
 7
 Rep Power
 0
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

Re: problem with recursive method
Just because you can solve it nonrecursively 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

Recursive Method
By emschorsch in forum New To JavaReplies: 11Last Post: 04182011, 06:01 AM 
recursive method
By michail in forum New To JavaReplies: 0Last Post: 01312010, 02:50 PM 
recursive method problem
By melody in forum New To JavaReplies: 1Last Post: 10292009, 08:15 AM 
Java Recursive method problem
By kj2009 in forum Advanced JavaReplies: 2Last Post: 02252009, 04:19 PM 
Recursive Method
By bluegreen7hi in forum New To JavaReplies: 5Last Post: 11292007, 05:45 AM
Bookmarks