0

I've having some trouble with recursion. At the moment, this code gives me an error message "missing return statement". Any way to get this working the way I want it to? I want it to calculate for xn and then return "count" when n reaches zero.

public class Question6p2 {
    public static void main(String[] args){
        int n = -6;
        int x = 2;
        int count = x;
        power2(n, x, count);
        System.out.println(power2(n, x, count));

    }

    public static int power2(int n, int x, int count){
        if (n != 0){
            if (n>0){
                count = count * x;
                n = n - 1;
            }
            else if (n<0)  {
                count = count * -x;
                n = n + 1;
            }
            power2(n, x, count);
        }
        else if (n == 0){
            return count;
        }
    }
}

Maybe I'm coming about this all wrong. Anyone care to help?

4
  • 1
    You need to return something when n is not zero. Commented Mar 29, 2014 at 19:45
  • I don't get what you do you want to do. You want to calculate x pow n ? (what is count) Commented Mar 29, 2014 at 19:46
  • @AnthonyRaymond I'm using count for storing x to the power of n. Commented Mar 29, 2014 at 21:32
  • if in the body of a method that returns int both branches needs a return statement that returns an int or you nned to return something after the if stamement. In main when you call the first time you don't do anything with the return. Commented Mar 30, 2014 at 0:24

3 Answers 3

3

Currently, you have this statement:

power2(n, x, count);

... which ignores the result completely. In that branch, we never return anything from the method call. I suspect these two issues are linked.

I suspect you just want:

return power2(n, x, count);
Sign up to request clarification or add additional context in comments.

Comments

2

Currently you are getting an error about not having a return statement because your return statement is within an if statement, so if that if statement doesn't run you will not return anything which is a problem.

Also I think you are going about recursion fundamentally wrong, as you are never calling back to your method recursively.

What you probably want to do within your power method is to accept n as the number of time to call your method, then lower it by 1 with each recursion. Then on every recursion multiply x by the original value.

Here is what I mean:

 public static double power2(int n, int x,int xOriginal){
    if(n == 0){
        return 1;
    }
    if(n < 0){
        return 1 / power2(n*-1, x, x);
    }
    if(n <= 1){
        return x;
    }
    return power2(n -1, x * xOriginal, xOriginal);
}

Edit: Works with negative n now.

Comments

1

There are a few things wrong with your algorithm:

What does it mean to have a negative exponent?

You should understand that x-n can be written 1 / xn. This is not what was reflected in your algorithm.

All possible cases

There are 4 basic cases when calculating exponents.

  • There is any value x0 = 1.
  • Any x1 = x
  • Any negative exponent x-n = 1 / xn
  • Any positive exponent greater than one: xn where n > 1

Your algorithm should return 1 when x has an exponent of zero. Return x when the exponent is 1 or recursively call the algorithm when n > 1.

In the special case where n < 0 (ie you have a negative exponent) You can simply return the reciprocal 1 / method() as long as you change the sign of n before calling the method.

The line:

else if (n < 0){
    n = -n;
    return(1 / power2(n, x, count));
}

Checks for negative exponents, and returns 1 / xn Take note that the sign of n changed here, and now this is operating like any other method call with positive exponents.

public class TestCode {
    public static void main(String[] args){
        int n = 4;
        int x = 5;
        double count = x;
        System.out.println(power2(n, x, count));

    }

    public static double power2(int n, int x, double count){
        if (n == 0)
            return 1;
        else{
           if (n > 1){
                count = count * x;
                n = n - 1;
            }
           else if (n < 0){
               n = -n;
               return(1 / power2(n, x, count));
           }
            else if (n == 1)  {
                return count;

            }
           return power2(n, x, count);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.