Posted on 1998-10-27
Last Modified: 2010-04-02
I have a recursive funtion that is supposed to
factor out the greatest common denominator.
long gcd(long x, long)
  return (x ==0L) ? y : gcd()y % x, x);
this works fine. I put two denominators in I get a number out
here is the question.
how I am I supposed to use this to add fraction.
if I put a 3,5 in I get a 1 out.
this is a silly question, I must be looking at this
at the wrong angle.
Please help.
Question by:strmtrpr
  • 3

Author Comment

ID: 1176374
that is supposed to be gcd(y % x, x);

Author Comment

ID: 1176375
that is supposed to be gcd(y % x, x);

Accepted Solution

newexpert earned 50 total points
ID: 1176376
You can do it exactly as you do fraction addition by hand.
a/b + c/d = ( a*d + b*c ) / (b*d)
Now the result is not the simplest form.  We can reduce it to the simplest form by dividing both top and bottom by their gcd.

a/b + c/d =  (A/gcd(A, B)) / (B/gcd(A, B)) where A = a*d + b*c and B = b*d
LVL 84

Expert Comment

ID: 1176377
a/b + c/d = (a*d+b*c)/(b*d) = ((a*d+b*c)/gcd(b,d)) / (b*d/gcd(b,d))

Author Comment

ID: 1176378
I told you it was a silly question.

thanks for the help

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now