-2

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 :/

tay10r
  • 4,234
  • 2
  • 24
  • 44
Boris
  • 65
  • 1
  • 1
  • 9
  • possible duplicate of [Recursive function that divides the number by 2 without using /](http://stackoverflow.com/questions/16996342/recursive-function-that-divides-the-number-by-2-without-using) – Mat Jun 08 '13 at 08:20
  • I know, but if no one reopens me the post, or helps me, what am I supposed to do?? – Boris Jun 08 '13 at 08:21
  • 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 Jun 08 '13 at 08:23
  • essentially you would like to make a `float dividebytwo (float a)` function without using `/`, correct? – tay10r Jun 08 '13 at 08:24
  • That's what I did, and it needs to be decimal so nope no float. – Boris Jun 08 '13 at 08:25
  • I see "int recursive(int a, int b)" what does it mean "he wants a float"? @Taylor so please get rid of your downvoting – Salvatore Jun 08 '13 at 08:31
  • I see a really negative feedback from this community :/ – Boris Jun 08 '13 at 08:32
  • @BorisWainstein That's probably because you're asking people to do your homework for you and also reposted the exact same question that was closed 2 hours ago as "not a real question". – j883376 Jun 08 '13 at 08:34
  • If this is not a question then what is a question? looks like I've arrived to the wrong planet. – Boris Jun 08 '13 at 08:36
  • If you want a function that takes one argument 'n', then why write a function that takes two arguments, 'a' and 'b'? – Paul Hankin Jun 08 '13 at 08:37
  • @j883376 And I'm not asking them to do my homework, I do it because I am having trouble, and I just can't get to understand how to do it.... even some tips would come good (If they are correct for sure). Looks like that when I felt that this would be the page to get help in case needed, etc in programming I came to totally the wrong place, so what is this page supposed to be then? – Boris Jun 08 '13 at 08:38
  • 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. – Paul Hankin Jun 08 '13 at 09:08
  • Homework problem, hmm? – devnull Jun 08 '13 at 09:10
  • Are you sure it can only recieve 1 parameters? If this is some kind of a homework, teaching recursion with codes that that is not completely encapsulated is usually counterproductive. – vinczemarton Jun 08 '13 at 09:16
  • @Anonymous Believe me I have no idea neither. And I'm really sorry for all the trouble caused. – Boris Jun 08 '13 at 14:01

4 Answers4

2

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;
   }
}
Salvatore
  • 1,145
  • 3
  • 21
  • 42
  • Thanks for the answer, but please read closely: returns "n/2" without dividing And it can receive only 1 parameter. – Boris Jun 08 '13 at 08:23
  • I see "int recursive(int a, int b)" what does it mean "he wants a float"? @Taylor – Salvatore Jun 08 '13 at 08:29
  • 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. – Salvatore Jun 08 '13 at 08:40
  • 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. – tay10r Jun 08 '13 at 09:04
1

This divides by two using repeated subtraction and recursion.

int divide_by_two(int a) {
    if (a < 0) return -divide_by_two(-a);
    if (a < 2) return 0;
    return 1 + divide_by_two(a - 2);
}

Generalising, this divides a by b using repeated subtraction and recursion.

int divide(int a, int b) {
    if (a < 0) return -divide(-a, b);
    if (b < 0) return -divide(a, -b);
    if (a < b) return 0;
    return 1 + divide(a - b, b);
}

Note, these functions don't round exactly the same way that division is defined to do in C.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
0

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!

tay10r
  • 4,234
  • 2
  • 24
  • 44
  • but with every --/++ the static answer will change, and if you don't write the static it will work too. – Boris Jun 08 '13 at 08:51
  • 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. – tay10r Jun 08 '13 at 09:00
  • It's not me who puts the rules xD – Boris Jun 08 '13 at 09:07
0
#include<stdio.h>
int divide(int a, int b){
    if( a-b < 0)
      return 0;
    else if ( a-b == 0)
      return 1;
    else {
      return divide(a-b, b) + 1;
    }
  }

http://codepad.org/o4CoiaON