Solved

fractions

Posted on 1998-10-27
5
309 Views
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.
0
Comment
Question by:strmtrpr
[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
  • 3
5 Comments
 

Author Comment

by:strmtrpr
ID: 1176374
that is supposed to be gcd(y % x, x);
sorry.
0
 

Author Comment

by:strmtrpr
ID: 1176375
that is supposed to be gcd(y % x, x);
sorry.
0
 
LVL 1

Accepted Solution

by:
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.
Therefore

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

Expert Comment

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

Author Comment

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

thanks for the help
0

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