How can I create a recursive function to mimic long division in C++?

In summary, to mimic division recursively in C++, you can use a function that performs long division by dividing the dividend by the divisor and outputting the result. The remainder is then multiplied by 10 and the process is repeated until the desired number of decimal places is reached. The function should call itself recursively and stop when all decimal places have been outputted.
  • #1
FallArk
127
0
When I do things like
Code:
cout << 1/3;
in C++, it will typically output 0.333333, but the actual answer should be 0.3333333 with an infinite amount of 3s, how should I make a function such that will mimic this division recursively?
 
Technology news on Phys.org
  • #2
FallArk said:
When I do things like
Code:
cout << 1/3;
in C++, it will typically output 0.333333, but the actual answer should be 0.3333333 with an infinite amount of 3s, how should I make a function such that will mimic this division recursively?

Hey FallArk! ;)

Do you know how to do long divisions?
That's the way to do it - with a recursive function to make it happen.

(Btw, your example code will output [m]0[/m] instead of [m]0.3333333[/m]. Know why?)
 
  • #3
I like Serena said:
Hey FallArk! ;)

Do you know how to do long divisions?
That's the way to do it - with a recursive function to make it happen.

(Btw, your example code will output [m]0[/m] instead of [m]0.3333333[/m]. Know why?)

I know it's int so it outputs 0. Since that's the integer part. But would you mind explaining the long division and how it works? Thank you!
 
  • #4
FallArk said:
I know it's int so it outputs 0. Since that's the integer part. But would you mind explaining the long division and how it works? Thank you!

Well... divide 1 by 3 as integers and output the result 0.
The remainder is 1.
Multiply the remainder by 10 and repeat.

Oh, and we need to output the decimal point after the first division.

More specifically:
\begin{array}{ccccc}
Dividend & Divisor & Quotient & Remainder & Output \\
1 & 3 & 0 & 1 & 0. \\
10 & 3 & 3 & 1 & 0.3 \\
10 & 3 & 3 & 1 & 0.33 \\
10 & 3 & 3 & 1 & 0.333 \\
...
\end{array}
 
  • #5
I like Serena said:
Well... divide 1 by 3 as integers and output the result 0.
The remainder is 1.
Multiply the remainder by 10 and repeat.

Oh, and we need to output the decimal point after the first division.

More specifically:
\begin{array}{ccccc}
Dividend & Divisor & Quotient & Remainder & Output \\
1 & 3 & 0 & 1 & 0. \\
10 & 3 & 3 & 1 & 0.3 \\
10 & 3 & 3 & 1 & 0.33 \\
10 & 3 & 3 & 1 & 0.333 \\
...
\end{array}

If I want to output to, let's say, 10 decimal places. And I do a recursive function:
Code:
double longDivision (int dividend, int divisor, int decimalplaces){} // Are the data types correct?
What would be the general case when the function returns an output?
I want to say that when the amount of numbers after the decimal matches
Code:
int decimalplaces
,
is that correct?
 
  • #6
FallArk said:
If I want to output to, let's say, 10 decimal places. And I do a recursive function:
Code:
double longDivision (int dividend, int divisor, int decimalplaces){} // Are the data types correct?
What would be the general case when the function returns an output?
I want to say that when the amount of numbers after the decimal matches
Code:
int decimalplaces
,
is that correct?

Looks fine to me.
Keep going! (Nod)

Oh, and the function shouldn't return a double.
Either it should return a string, or it should simply output the result to cout (and return void).
 
  • #7
I like Serena said:
Looks fine to me.
Keep going! (Nod)

Oh, and the function shouldn't return a double.
Either it should return a string, or it should simply output the result to cout (and return void).

Cool! I will keep working on it!
 
  • #8
Since making it print infinite decimal places would be pointless as that's the equivalent of an infinite loop why not just use the iomanip library or printf to output the number of decimal places you want to be output.
 
  • #9
squidsk said:
Since making it print infinite decimal places would be pointless as that's the equivalent of an infinite loop why not just use the iomanip library or printf to output the number of decimal places you want to be output.

Because:
Code:
#include <stdio.h>
int main() {
    printf("%.20f\n", 1.0/3);
    return 0;
}
results in:
Code:
0.33333333333333331483
 
  • #10
I like Serena said:
Looks fine to me.
Keep going! (Nod)

Oh, and the function shouldn't return a double.
Either it should return a string, or it should simply output the result to cout (and return void).

I know how to do long division but how should I make it recursive?
Code:
void longDivision(int dividend, int divisor, int decimalplace){
    int x = dividend / divisor;
    cout << x;
    dividend = (dividend - (x * divisor)) * 10;
    cout << dividend / divisor;
}
 
  • #11
FallArk said:
I know how to do long division but how should I make it recursive?
Code:
void longDivision(int dividend, int divisor, int decimalplace){
    int x = dividend / divisor;
    cout << x;
    dividend = (dividend - (x * divisor)) * 10;
    cout << dividend / divisor;
}

Instead of the 2nd cout, we should have a call to longDivision(dividend, divisor, decimalPlaces -1).
Oh, and there should be an if to stop doing that when we have all the decimal places.
 
  • #12
I like Serena said:
Instead of the 2nd cout, we should have a call to longDivision(dividend, divisor, decimalPlaces -1).
Oh, and there should be an if to stop doing that when we have all the decimal places.

Duh! How did I not see that!
Thank you!
And here is my solution:
Code:
void longDivision(int dividend, int divisor, int decimalplace){
    if (decimalplace == 0) {
        return;
    }
    else {
        cout << dividend / divisor;
        longDivision((dividend % divisor) * 10, divisor, decimalplace - 1);
    }
}
:D
 
  • #13
You can clean up your solution by inverting the if statement as follows:

Code:
void longDivision(int dividend, int divisor, int decimalplace){
    if (decimalplace > 0) {
        cout << dividend / divisor;
        longDivision((dividend % divisor) * 10, divisor, decimalplace - 1);
    }
}

This eliminates the else and the empty return. In general it is better to have a single point of exit from a function.
 

Related to How can I create a recursive function to mimic long division in C++?

1. What is recursion in C++?

Recursion in C++ is a process where a function calls itself repeatedly until a certain condition is met. This allows for solving complex problems by breaking them down into smaller, simpler versions of the same problem.

2. How does recursion work in C++?

Recursion works by breaking down a problem into smaller versions of the same problem, until a base case is reached. The base case is a condition that stops the function from calling itself again, and allows it to return a result. This result is then used to solve the previous levels of the problem until the original problem is solved.

3. What are the advantages of using recursion in C++?

Some advantages of using recursion in C++ include writing more concise and elegant code, solving complex problems more efficiently, and easier debugging as the code is broken down into smaller parts. Additionally, certain problems are easier to solve using recursion compared to other methods.

4. What are the limitations of using recursion in C++?

One limitation of using recursion in C++ is that it can lead to stack overflow if the recursive function is called too many times, as each function call takes up memory on the stack. Additionally, if the base case is not properly defined, the function may continue to call itself infinitely, leading to an infinite loop.

5. How can recursion be optimized in C++?

Recursion in C++ can be optimized by using tail recursion, where the recursive call is the last statement in the function. This allows the compiler to optimize the code and potentially reduce the risk of stack overflow. Additionally, using dynamic programming techniques can also improve the efficiency of recursive algorithms.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
11
Views
932
  • Programming and Computer Science
Replies
5
Views
12K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top