Home > Software engineering >  reset variable in recursive method back to zero after its done/before its called again?
reset variable in recursive method back to zero after its done/before its called again?

Time:11-20

I was given the task to code the following recursive method. midresult is supposed to be 0 when I call the method. It works if I call the method just once, but because I have this midresult variable, as soon as I call it more than once in a row it returns wrong values because it adds up midresult.

How do I set back midresult to 0 each time after the method is done running? Im not allowed to put it in my main method, but I can't put it into the actual recursive method because this will mess up the recursion right?

eg for x=5, y=9 the result should be 15, which works if I only call the method once. But if I call it with x=5 and y=9 after calling it with other xy values the return value is wrong.

static int midresult;

public static int recursivemethod(int x, int y) {
   // TODO
   if(x==0) {
      return y   midresult;
   }
   else{
      if((x midresult)%2==0) {
         midresult = (x/2);
         int temp= y;
         y=(x/2);
         x=temp;
         return recursivemethod(x, y);
      }
      else {
         midresult = y;
         x-=1;
         y=(y/2);
         return recursivemethod(x, y);
      }
   }
}

CodePudding user response:

You're storing recursion results in a global variable. It is initialized to 0; this is why your first function call is working; after that, however, whatever value is stored in midresult afterwards is what is used by later function calls. You mentioned you weren't allowed to modify main, so try changing your recursive base case

if(x == 0) {
    return y   midresult;
}

to this

if(x == 0) {
    int temp = y   midresult;
    midresult = 0;
    return temp;
}

CodePudding user response:

I would not use a static variable to hold midresult. In some circumstances, that can cause problems. An alternative is to use a helper method to pass midresult as an argument. That value will be passed to the helper when the original method is invoked. Then you don't need to worry about resetting it for subsequent use.

public static int recursivemethod(int a, int b) {
    return recursivemethod(a, b, 0);
}

private static int recursivemethod(int x, int y, int midresult) {
    // TODO
    if (x == 0) {
        return y   midresult;
    } else {
        if ((x   midresult) % 2 == 0) {
            midresult  = (x / 2);
            int temp = y;
            y = (x / 2);
            x = temp;
            return recursivemethod(x, y, midresult);
        } else {
            midresult  = y;
            x -= 1;
            y = (y / 2);
            return recursivemethod(x, y, midresult);
        }
    }
}

Of course, this might be avoided too if you explain the overall algorithm you are trying to recursively implement. You can edit your question to do this. Just don't change your existing code/approach as answers and comments have been provided based on that code.

  • Related