Division using Recursion

I need to write a function dividing 2 numbers using recursion. I cant use any loops or the division, multiplication, or modulus operators. This is the code ive been given to work with.
1
2
3
4
5
6
7
8
9
  void divide(int dividend, int divisor, in &quotient, int  &remainder)
{
}
int main()
{
     int x,y;
     divide(65,12,x,y);
     return 0:
}

Im not really sure where to start, im very clueless as to how to work with the "&quotient , &remainder". I need to use those in my code but i dont know how. Any help would be greatly appresciated
Remember that division is just repeated subtraction.

I'm going to modify your example by one.

64 ÷ 12

64 - 12 = 52 (subtracted 12 one time)
52 - 12 = 40 (subtracted 12 two times)
40 - 12 = 28 (subtracted 12 three times)
28 - 12 = 16 (subtracted 12 four times)
16 - 12 = 4 (subtracted 12 five times)

4 < 12, so I cannot subtract any more twelves.

Therefore, 64 ÷ 12 = 5 with a remainder of 4.

[edit]
Oh, also, the & indicates a reference.
https://isocpp.org/wiki/faq/references#overview-refs

I would rewrite your main function as:

1
2
3
4
5
6
7
int main()
{
    int q, r;
    divide(65,12,q,r);
    cout << "65 / 12 = " << q << " with a remainder of " << r << ".\n";
    return 0;
}


Hope this helps.
Last edited on
Topic archived. No new replies allowed.