I have the following program to reduce rational numbers but am getting error messages at the lines x = c.subtraction( d ); and for multiplication, addition and division that says no operator "=" matches these operands. I do not know why. Could someone please help?

class Rational
{
private:
int numerator;
int denominator;
int d;
int x;

public:
Rational(int num = 0, int den = 1);
void addition(Rational c);
void subtraction(Rational c);
void multiplication(Rational c);
void division(Rational c);
void printRational();
void printRationalAsDouble();

private:
void reduction();
};

Rational::Rational(int num, int den) : numerator(num), denominator(den)
{
}

void Rational::addition(Rational c)
{
numerator = numerator + c.numerator;
denominator = denominator + c.denominator;
reduction();
}


void Rational::subtraction(Rational c)
{
numerator = numerator - c.numerator;
denominator = denominator - c.denominator;
reduction();
}

void Rational::multiplication(Rational c)
{
numerator = numerator * c.numerator;
denominator = denominator * c.denominator;
reduction();
}
#include "Rational.h" // include definition of class Rational

int main()
{
   Rational c( 2, 6 ), d( 7, 8 ), x; // creates three rational objects 

   c.printRational(); // prints rational object c
   cout << " + ";
   d.printRational(); // prints rational object d				
   x = c.addition( d ); // adds object c and d; sets the value to x

   cout << " = ";
   x.printRational(); // prints rational object x
   cout << '\n';
   x.printRational(); // prints rational object x    
   cout << " = ";
   x.printRationalAsDouble(); // prints rational object x as double
   cout << "\n\n";

   c.printRational(); // prints rational object c
   cout << " - ";
   d.printRational(); // prints rational object d
   x = c.subtraction( d ); // subtracts object c and d 
           
   cout << " = ";
   x.printRational(); // prints rational object x
   cout << '\n';
   x.printRational(); // prints rational object x
   cout << " = ";
   x.printRationalAsDouble(); // prints rational object x as double
   cout << "\n\n";

   c.printRational(); // prints rational object c
   cout << " x ";
   d.printRational(); // prints rational object d
   x = c.multiplication( d ); // multiplies object c and d
                            
   cout << " = ";
   x.printRational(); // prints rational object x
   cout << '\n';
   x.printRational(); // prints rational object x
   cout << " = ";
   x.printRationalAsDouble(); // prints rational object x as double
   cout << "\n\n";

   c.printRational(); // prints rational object c
   cout << " / ";
   d.printRational(); // prints rational object d	
   x = c.division( d ); // divides object c and d
                            
   cout << " = ";
   x.printRational(); // prints rational object x		
   cout << '\n';
   x.printRational(); // prints rational object x
   cout << " = ";
   x.printRationalAsDouble(); // prints rational object x as double
   cout << endl;
          system("pause");
   return 0;
} // end main

The process is pretty much what you would expect: first you find the Greatest Common Divisor of the two integral parts, and then divide each by that. The result will be the reduced form of the rational number.

The easiest way to find the GCD of a number is the recursive form of Euclid's Algorithm:

int gcd(int a, int b)
{
    if b == 0
       return a;
    else
       return gcd(b, a % b);
}

(I would have left this as pseudo-code for you to convert to C++, but the algorithm is so trivial to code that it was just as easy to give it to you whole cloth rather than force you to jump through pointless hoops.)

I understand the GCD part of it and the code to find the GCD was actually provided to me but I keep getting error messages as lines 10, 23, 36, 49 that will not allow me to put x = to the math concept. I'm assumming it is something in the header file that I need to change because the entire main file was provided to me as well but I can't figure out where I need to make the change.

OK, I've look at the code now, and I see where the problem lies now. The issue is that the functions in question never return a value to be assigned in the first place! All of your functions are declared and coded as void , which isn't right at all. You want the functions to return a value, rather than changing the value of the object being operated on.

I changed the code to what I have below and now it is compiling and running but not giving the correct answers.

class Rational
{
private:
int numerator; // initializes numerator
int denominator; // initializes denominator
double d; // initializes d
double x; // initializes x

public:
Rational(int num = 0, int den = 1);
double addition(Rational c);
double subtraction(Rational c);
double multiplication(Rational c);
double division(Rational c);
void printRational();
void printRationalAsDouble();

private:
double reduction();
};

Rational::Rational(int num, int den) : numerator(num), denominator(den)
{

}

double Rational::addition(Rational c)
{
numerator = numerator + c.numerator; // adds numerator
denominator = denominator + c.denominator; // add denominator
return reduction();
}


double Rational::subtraction(Rational c)
{
numerator = numerator - c.numerator; // subtracts numerator
denominator = denominator - c.denominator; // subtracts denominator
return reduction();
}

double Rational::multiplication(Rational c)
{
numerator = numerator * c.numerator; // multiplies numerator
denominator = denominator * c.denominator; // multiplies denominator
return reduction();
}

double Rational::division(Rational c)
{
numerator = numerator * c.denominator; // divides numerator and denominator
denominator = denominator * c.numerator; // divdes denominator and numerator
return reduction();
}

void Rational::printRational()
{
	cout << numerator << "/" << denominator; // prints fraction
}

void Rational::printRationalAsDouble()
{
	cout << d / static_cast<float>( x ); // prints as a double number
}


double Rational::reduction()
{
   int largest; // initializes largest
   largest = numerator > denominator ? numerator : denominator;

   int gcd = 0; // greatest common divisor

   for ( int loop = 2; loop <= largest; loop++ )

       if ( numerator % loop == 0 && denominator % loop == 0 )
          gcd = loop;

   if (gcd != 0) 
   {
      numerator /= gcd;
      denominator /= gcd;
   } // end if 

   return gcd;
} // end function reduction

Sorry the code is supposed to output:
1/3 + 7/8 = 29/24
29/24 = 1.20833

1/3 - 7/8 = -13/24
-13/24 = -0.541667

1/3 x 7/8 = 7/24
7/24 = 0.291667

1/3 / 7/8 = 8/21
8/21 = 0.380952

but instead outputs:
2/6 + 7/8 = 0/1
0/1 = 0

9/14 - 7/8 = 2/1
2/1 = 0

1/3 * 7/8 = 0/1
0/1 = 0

7/24 / 7/8 = 56/1
56/1 = 0

And it does output...?
If you want help, you need to give ALL the details needed to understand the problem.

Yes it does output...I posted above what it outputs but it is not the correct output and I am not sure how to get to the correct output.

OK, one problem has to do with the process of addition (and subsequently, subtraction): when adding two rational fractions of different denominators, such as:

1/2 + 2/3

you need to put them into a common denominator. The easiest way to do this is by multiplying both the numerator and denominator by the other fraction's denominator:

1/2 * 3/3 = 3/6

2/3 * 2/2 = 4/6

At that point, you simply add the numerators and reduce:

3/6 + 4/6 = 7/6

then reduce, if possible.

Finally, you come across the final error: you want the functions to return a Rational result, not a double . So, what you really want is:

Rational Rational::addition(const Rational& c)
{
    Rational temp;

    temp.numerator = (numerator * c.denominator) + (c.numerator * denominator);
    temp.denominator = denominator * c.denominator;
    return temp.reduction();
}

However, there's still a tricky part to this: you'll want to overload the assignment operator to ensure that this works correctly. I'm guessing that overloading operators has not been covered, however, or else the code would have been written to use them.

Edited 5 Years Ago by Schol-R-LEA: n/a

This article has been dead for over six months. Start a new discussion instead.