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

  • Context: C/C++ 
  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    C++ Recursion
Click For Summary
SUMMARY

The discussion focuses on creating a recursive function in C++ to mimic long division, specifically for representing repeating decimals. Participants emphasize the need to handle integer division correctly, outputting the decimal point after the first division, and recursively calculating the decimal places. The final solution provided is a function that outputs the quotient and recursively calls itself to handle the remainder, ensuring a clean exit point in the function structure.

PREREQUISITES
  • Understanding of C++ syntax and data types
  • Knowledge of recursion in programming
  • Familiarity with integer division and remainder operations
  • Experience with outputting formatted data in C++ using cout
NEXT STEPS
  • Implement and test the provided recursive long division function in C++
  • Explore the use of the iomanip library for controlling decimal output
  • Learn about handling infinite loops and recursion limits in C++
  • Investigate alternative methods for representing repeating decimals in programming
USEFUL FOR

C++ developers, computer science students, and anyone interested in understanding recursion and numerical representation in programming.

FallArk
Messages
127
Reaction score
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
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?)
 
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!
 
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}
 
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?
 
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).
 
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 count (and return void).

Cool! I will keep working on it!
 
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.
 
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.
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
12K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
81
Views
7K
Replies
1
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K