Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to create a recursive function that receives a number by two without using /.

This is what I wrote, but it works only if after dividing it will still be a decimal number and not a float, that's why I asked.

int recursive(int a, int b){
  if ( a == (0.5 * b) )
    return a;
  return recursive(a-1, b);
}

Btw, the function can receive only 1 parameter not 2 or more :/

share|improve this question
I know, but if no one reopens me the post, or helps me, what am I supposed to do?? – Boris Wainstein 1 hour ago
Edit your previous question to make it clearer. I'm not sure I understand the part about floats up there. Do not repost your questions. – Mat 1 hour ago
essentially you would like to make a float dividebytwo (float a) function without using /, correct? – Taylor Flores 1 hour ago
1  
@BorisWainstein you've explained your problem very poorly, making it hard to impossible for anyone to give you a satisfactory answer. I literally have no idea what you're asking for. – Anonymous 22 mins ago
show 9 more comments

2 Answers

up vote 0 down vote accepted

You can try this, it should work:

int dividebytwo (int a){
    static int answer = 0;

    if (a < 0){
        if ((answer * 2) > a){
            answer --;
            return dividebytwo (a);
        }

        return answer;

    } else if (a > 0){
        if ((answer * 2) < a){
            answer ++;
            return dividebytwo (a);
        }

        return answer;
    }

    return 0;
}

The trick here is using the static attribute. The static attribute means that the variable is only initialized once and retains its value after every function call. Really, you're using two parameters but it looks like you're only using one.

The only downside to this function is that you can only count on it to work more than once. Since this is probably for a simple homework assignment, it probably doesn't matter. But in reality, this is considered hugely inefficient and unreliable.

To compensate for the only-works-once factor, may add one of these fixes:

  • declare answer as a global variable and set it to 0 before every function call.

  • append return answer = 0; to the end of the function, instead of return 0;. This is so that whenever you want to call it again, you would call it beforehand as dividebytwo (0);.

I also cannot stress enough how weird of a concept this is, it sets of all sorts of red flags for anyone who practices careful programming - could be why you're getting so many downvotes. So use with caution!

share|improve this answer
Really thank you, I just don't have comments, thank you. – Boris Wainstein 47 mins ago
but with every --/++ the static answer will change, and if you don't write the static it will work too. – Boris Wainstein 40 mins ago
try taking out static and see what happens, you will get a stack overflow. So no, it will not work without static. Also, I've added some info in my answer you should read. – Taylor Flores 31 mins ago
It's not me who puts the rules xD – Boris Wainstein 23 mins ago

I think you need something like this

int divide(int a, int b){
   if(a - b <= 0){
      return 1;
   }
   else {
      return divide(a - b, b) + 1;
   }
}
share|improve this answer
Thanks for the answer, but please read closely: returns "n/2" without dividing And it can receive only 1 parameter. – Boris Wainstein 1 hour ago
It returns a decimal – Boris Wainstein 1 hour ago
I see "int recursive(int a, int b)" what does it mean "he wants a float"? @Taylor – salvo 1 hour ago
I think one solution could be to find a series which converges to n/2 given n...there should be one but I don't remember. – salvo 51 mins ago
I had thought he wanted a float value when he said decimal, but the down-vote is also because he mentioned he only wanted one parameter. – Taylor Flores 27 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.