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
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
  • 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

Industry Leaders: 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!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

617 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