Tuesday, 17 September 2013

Converting a fraction into a decimal with integers in C++

Converting a fraction into a decimal with integers in C++

Yes. It's an assignment, that is quite a brain teaser for me to be
honest.. The goal of the program, or "problem" (if you want to look at it
in math terms) is dividing two numbers against each other. You are able to
divide whole numbers, and fractions against each other. The function
definition looks like this:
bool divide(int c1, int n1, int d1, int c2, int n2, int d2, char result[],
int len)
c1, c2 - Whole numbers
n1, n2 - Numerator 1, Numerator 2
d1, d2 - Denominator 1, Denominator 2
result[ ] - The character array that is to display the answer
len - The number of characters allowed in result []



I would simply use long division and find my answer this way but since
there is a restriction to not use double, float, or string I am more
limited in my options towards my approach.
The good news is I've gotten a far ways towards the end solution and would
like to ask for advice as what my next move might be. This is my process
thus far:
1) Convert each number to an improper fraction
2) Take result 1 * (1 / result 2 )
3) Find the whole number part of the solution (if there is one)
4) (From the improper fraction) Take the numerator % denominator to find
my new numerator for the mixed fraction I have
5) I am now here, trying to find a base 10 multiple for the denominator so
I can represent the mixed fraction in decimal format.. Any pointers would
be helpful!

No comments:

Post a Comment