Help with calling the reduce function
Apr 10, 2013 at 5:51pm UTC
The program works, but the rational number doesn't print as reduced. I guess i have to call the reduction function, but i'm not sure where. can someone help?
the output that i get is:
2/6 + 7/8 = 58/48
58/48 = 1.20833
2/6 - 7/8 = -26/48
-26/48 = -0.541667
2/6 x 7/8 = 14/48
14/48 = 0.291667
2/6 / 7/8 = 16/42
16/42 = 0.380952
the output that i suppose to get is:
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
here is my header file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//Prevent multiple inclusions of header
#ifndef RATIONAL_H
#define RATIONAL_H
//Rational class definition
class Rational
{
public :
Rational(int , int );//constructor
Rational();
void reduction(); //function to reduce the rational number
Rational addition(Rational &); //add rational number
Rational subtraction(Rational &); //subtract rational number
Rational multiplication(Rational &); // multiply rational number
Rational division(Rational &); // divide rational number
void printRational(); //print rational number
void printRationalAsDouble(); //print rational number as decimal
private :
int numerator;
int denominator;
};
#endif
here is my source file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#include <iostream>
using namespace std;
#include "Rational.h" // include definition of class Rational
void Rational::reduction()
{
int 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
} // end function reduction
Rational::Rational(int a, int b)
{
numerator = a;
denominator = b;
}
Rational::Rational()
{
}
Rational Rational::addition(Rational &a)
{
int newDenominator = denominator * a.denominator;
int newNumerator = (numerator * a.denominator) + (denominator * a.numerator);
Rational newValue(newNumerator, newDenominator);
return newValue;
}
Rational Rational::subtraction(Rational &a)
{
int newDenominator = denominator * a.denominator;
int newNumerator = (numerator * a.denominator) - (denominator * a.numerator);
Rational newValue(newNumerator, newDenominator);
return newValue;
}
Rational Rational::multiplication(Rational &a)
{
int newDenominator = denominator * a.denominator;
int newNumerator = numerator * a.numerator;
Rational newValue(newNumerator, newDenominator);
return newValue;
}
Rational Rational::division(Rational &a)
{
int newDenominator = denominator * a.numerator;
int newNumerator = numerator * a.denominator;
Rational newValue(newNumerator, newDenominator);
return newValue;
}
void Rational::printRational()
{
cout << numerator << "/" << denominator;
}
void Rational::printRationalAsDouble()
{
cout << static_cast <double >(numerator)/denominator;
}
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
Apr 10, 2013 at 9:45pm UTC
You could put newValue.reduction();
before all your return newValue;
in your Rational arithmetic methods.
Or, you could just call reduction();
on the first line of your printRational()
method.
Last edited on Apr 10, 2013 at 10:10pm UTC
Apr 11, 2013 at 1:14am UTC
I have tried these two before and it didn't work it still doesn't reduce the rational numbers
Apr 11, 2013 at 1:25am UTC
never mind i guess i was thinking to hard actually i was trying reduction() by itself which was my error. It suppose to be Rational::reduction(); I tried so many syntax by the end i wasn't sure anymore.
I change my code to this:
1 2 3 4 5 6
void Rational::printRational()
{
Rational::reduction();
cout << numerator << "/" << denominator;
}
and now it works thank for the insight
Topic archived. No new replies allowed.