### Debugging issue regarding fractions

The code below is designed to add, subtract, multiply, and divide fractions created in the class Rational. The class definition is in a header file and is fine. Also, int main() was supplied, and must not be changed. The two problems found are:

1. the output of the functions (i.e. what x equals) is always constant, being equal to the second fraction (i.e. "d").

2. The first fraction in all functions following addition() is "1/8", or , in general "1/[denominator of d]".

Here is the code in question

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116`` ``````void Rational::printRational() // prints a single fraction { cout << numerator << "/" << denominator; } void Rational::printRationalAsDouble() // prints a fraction as a decimal { double function = static_cast (numerator)/static_cast (denominator); cout << function; } Rational Rational::addition(Rational y) // adds fractions { int numerSum; numerSum = (numerator * y.getDenominator()) + (denominator * y.getNumerator()); if (denominator <= y.getDenominator()) { denominator = y.getDenominator(); } return y; } Rational Rational::subtraction(Rational y) // subtracts fractions { int numerDifference; numerDifference = (numerator * y.getDenominator()) - (denominator * y.getNumerator()); if (y.getDenominator() >= denominator) { y.setDenominator(denominator); } return y; } Rational Rational::multiplication(Rational y) // multiples fractions { int numerProduct; int numerProduct2; numerProduct = (numerator * y.getNumerator()); numerProduct2 = (denominator * y.getDenominator()); return y; } Rational Rational::division(Rational y) // divides fractions { int numerQuotient; int numerQuotient2; numerQuotient = (numerator * y.getDenominator()); numerQuotient2 = (denominator * y.getNumerator()); return y; } int main() { Rational c( 3, 6 ); Rational d( 7, 9 ); Rational x; // creates three rational objects c.reduction(); d.reduction(); 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; }``````

Any and all help will be welcomed.
This function

 ``12345678910`` ``````Rational Rational::addition(Rational y) // adds fractions { int numerSum; numerSum = (numerator * y.getDenominator()) + (denominator * y.getNumerator()); if (denominator <= y.getDenominator()) { denominator = y.getDenominator(); } return y; }``````

has no any sense. The function gets as an argument an object of type Rational and returns the same object without any changes of it. So what you will pass to the function as an argument the same will be returned by the function.

Apart from this as far as I know denominator of the sum is calculated as a product of two denominators.
Last edited on
Topic archived. No new replies allowed.