• 04-29-2009
CaliJoe
We have everything done except we do not know how to do the adding of rational numbers. can you help?

Code:

```#include "Rational.h" #include "math.h" #include <iostream> using namespace std; Rational::Rational() {     numerator = denominator = 1; } Rational::Rational(int N, int D) {     numerator = N;     denominator = D; } int Rational::getN() {               return numerator; } int Rational::getD() {               return denominator; } int Rational::multiply(Rational rational2) {     int finalN =getN()*rational2.getN();     int finalD =getD()*rational2.getD();     Rational rational(finalN,finalD);     simplify(rational); } int Rational::divide(Rational rational2) {     int finalN =getN()*rational2.getD();     int finalD =getD()*rational2.getN();     Rational rational(finalN,finalD);     simplify(rational); } int Rational::add(Rational rational2) { int tempN = getN()*rational2.getD(); int tempD = getD()*rational2.getN(); int finalN = tempN*tempD int finalD = Rational rational(finalN,finalD); simplify(rational); } void Rational::simplify(Rational rational) {     int x=rational.getN();     int y=rational.getD();     int z;     while (y!=0)     {           z=x%y;           x=y;           y=z;     }     int finalN=rational.getN()/x;     int finalD=rational.getD()/x;     Rational rationalnew (finalN,finalD);     cout <<finalN<<"/"<<finalD<<endl; }             bool Rational::compare(Rational rational2) {     float initial=(float)getN()/getD();     float final= (float)rational2.getN()/rational2.getD();     if (initial==final) return true;     else return false; }```
• 04-29-2009
computerquip
Simply put, multiply the nominators by the cross denominator and get a common denominator by multiplying the denominators together.

Adding and Subtracting Rational Functions - Free Math Help

An easy way to find a common denominator is by multiplying the denominators together:

Code:

```1/4 + 3/9 9/36 + 12/36 // 1 times 9 = 9. 3 times 4 = 12. 4 times 9 equal 36```
Knowing this, you should be able to find or make a function that does this for you.
If you need more help, just reply and I'll write some code.
• 04-29-2009
cyberfish
Quote:

Code:

```int Rational::multiply(Rational rational2) {     int finalN =getN()*rational2.getN();     int finalD =getD()*rational2.getD();     Rational rational(finalN,finalD);     simplify(rational); }```

Why not make simplify() a function that operates on "this" rational? That makes more sense to me. Unless the interface is part of the assignment that you can't change, of course.

Quote:

Code:

`if (initial==final) return true;`

This is dangerous because
1. the difference can be smaller than representable in a float
2. floats (and doubles) should generally not be compared with == because not all values can be represented with infinite precision

For this function, I would just simplify both functions, and see if the denominators and numerators are equal.

Also, for simplify(), I would "float" the negative sign to the top, if there is one. So "1/(-4) == (-1)/4" will be true.

Quote:

If you need more help, just reply and I'll write some code.
Just in case... please don't write people's homework for them.
• 04-30-2009
brewbuck
Quote:

Originally Posted by cyberfish
For this function, I would just simplify both functions, and see if the denominators and numerators are equal.

I assume you mean "fraction" not "function." I would not simplify them. I'd just check if numer1 * denom2 == numer2 * denom1.
• 04-30-2009
cyberfish
Yes (to both) of course :D.