c++ math stuff

Umm, I need to write a class for this assignment. The class has to compute fractions. I know how to do it, however, the answer must be reduced to lowest terms and that I do not know how to accomplish with the compiler. If someone could tell me how to go about this I would be really really grateful. The compiler I am using complies to the new standard and it is for unix.
a_wilso1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

snifongCommented:
Well you want to reduce the numerator(N) and denominator(D) by finding a factor that is common to both. The number that you are looking for is between 2 and the the smaller of the N or D divided by 2.
fraction& reduce(fraction& f) {
   for (int i = 2; i < min(f.numerator, f.denominator) / 2; i++) {
      while (f.numerator % i == 0 && f.denominator % i == 0) {
         f.numerator \= i;
         f.denominator \= i;
      }
   }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
snifongCommented:
Sorry. I saw a couple of mistakes.  Here is the revised version.
void reduce(fraction& f) {
   for (int i = 2; i <= __min(f.numerator, f.denominator); i++) {
      while (f.numerator % i == 0 && f.denominator % i == 0) {
         f.numerator /= i;
         f.denominator /= i;
      }
   }
}
This assumes you have the __min function.  It is in the stdlib.h header file.  This mean you will need #include <stdlib.h>

About the return type and parameters...
If it is in a class create it as a member function and you will not need parameters.  The function will modify the N and D of the class.
0
a_wilso1Author Commented:
Hey thanks. This really helped
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

nietodCommented:
That works, but there are much better (much faster) ways to find the greatest common factor (GCF) of two numbers.  A good book on algorithms will suggest some.  
0
nietodCommented:
Actually, that doesn't always work.  Consider what it would do to 4/8.  I would become 2/4 but not 1/2.  

A quick fix would be to skip the increment of i whenever you find a common factor.  That would make it work, but it still would be inneficient.

>> The number that you are looking for is
>> between 2 and the the smaller of the N
>> or D divided by 2
Actually, the GCF, which is what you want, woudl be between 1 and the positive difference (subtraction) between the N and D.  That is the key to many GCF algorithms.  So you might consider working backwards (not starting at the low value, like 1 or 2, but instead start at the high end and decrement i.  This will yeld the solution faster, but there still are faster ways, I just don't remember them.   (They usually involve subtraction rather than division and rapidly converge on a solution.)
0
snifongCommented:
nietod,
Thanks for the additions.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.