Solved

Need math function (or algorithm) for reducing fractions

Posted on 1998-04-19
10
395 Views
Last Modified: 2008-02-01
I need to find a C++ math library function for reducing fractions, but have not been able to find one. Is there one available? If not, can you help me with an algortihm to do this i.e. how to get an answer of 3/5 when the input was 6/10?
0
Comment
Question by:englm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 3

Accepted Solution

by:
q2guo earned 50 total points
ID: 1162143
The following function is not the most efficient funciton for doing fraction reduction.  But its the only one I can come
up with in 2 minutes.

The argument d and n represent denominator and neminator respectively.


void Reduce(int &d, int &n) {
    for (int i = n/2; i > 1; i--) {
        if ( (n%i) == 0) {
            if ( (d%i) == 0) {
                 n = n / i;
                 d = d / i;
                 break;
            }
        }
    }
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 1162144
//Heres one Euclid came up with over 2 millennia ago
int gcd(int a,int b){
  while( b ){
    int c;
    c = a%b;
    a = b;
    b = c;
  }
  return a;
}

main(int argc,char *argv[]){
   int d,n,f;
   d = atoi(argv[1]);
   n = atoi(argv[2]);
   f = gcd(d,n);
   printf("%d/%d\n",d/f,n/f);
}

0
 
LVL 84

Expert Comment

by:ozo
ID: 1162145
(If n was supposed to represent the numerator, I think I got my variable names backwards)

But it looks like Reduce(4,4) will only reduce to 2/2
and Reduce(4,-4) seems to have an even worse problem.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:Ready4Dis
ID: 1162146
Why (q2guo) do you start at n/2? Shouldn't you check to see which is heigher?  What if the fraction is 10/2.? It will return 10/2 which can be 5/1...
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1162147
Ready4Dis, I assumed that the fraction is a pure one, meaning that the numerator is always smaller that the denominator.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1162148
Then if n is assumed to be smaller than d, shouldn't you have started with d/2?
(I think that's what confused my labels:-) fortunately, the gcd approach doesn't depend such assumptions.
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1162149
ozo: why would you start with d/2?  Since n is smaller than d
all i(s) between d/2 and n/2 are just extra work.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1162150
Given a fraction like 2/10, with the numerator 2 smaller than the denominator 10,
starting i=n/2 i.e. i=1 will fail to reduce it to 1/5
(with a fraction like 10/2, since n=10 and d=2, i=n/2 would start at 5, so Reduce(2,10) would find (10/2)/(2/2) = 5/1)

0
 
LVL 3

Expert Comment

by:q2guo
ID: 1162151
Ok, here is the function again

void Reduce(int &d, int &n) {
        int temp = (d >= n ? d : n);
        for (int i = temp; i > 1; i--) {
            if ( (n%i) == 0) {
                if ( (d%i) == 0) {
                     n = n / i;
                     d = d / i;
                     break;
                }
            }
        }
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 1162152
Still has a problem with -10/2
Here's my (Euclid's) function again with the variables renamed:

int gcd(int a,int b){
  while( b ){
    int c = a%b;
    a = b;
    b = c;
  }
  return a;
}

main(int argc,char *argv[]){
   int n,d,i;
   n = atoi(argv[1]);
   d = atoi(argv[2]);
   i = gcd(n,d);
   printf("%d/%d\n",n/i,d/i);
}


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question