# can't get this function to reduce fractions

• 11-12-2002
bruce
can't get this function to reduce fractions
Hello,

i have been working on this for a while now. The program works

but i am getting the wrong output. I know it is something wrong

with the Reduce function in Fraction.h because i did a

system("pause") right before I called the Reduce function and the

I would greatly appreciate it if someone could look at it and let me know what' s wrong.

---Thank you, bruce

This is the .cpp file

Code:

```#include<iostream.h> #include"Fraction.h" const int MAX = 7; //-------------------------------------------------------------------------------- // gets the fractions from user int main() {         char ans;         do         {                 Fraction input[MAX];                         cout << "Enter 7 fractions and press Enter after each one: " << endl;                         for (int i = 0; i < MAX; i++)                 {                         input[i].Get();                 }                         cout << endl << endl; //--------------------------------------------------------------------------------                 //calculates sum and displayes the reduced sum                 Fraction sum;                 for (int i = 0; i < MAX; i++)                 {                         sum = sum+ input[i];                 }                                 sum = sum.Reduce(sum);                 cout << "The sum of those seven fractions is ";             sum.Show();         //-----------------------------------------------------------------------------------         //calculates product and displayes the reduced product                 Fraction product(1,1);                 for (int i = 0; i < MAX; i++)                 {                         product = product* input[i];                                }                                 product = product.Reduce(product);                 cout << "The product of those seven fractions is ";                 product.Show();         //--------------------------------------------------------------------------------------                 //increments product by 1, then displays                 cout << "The product added by 1 equals ";                 product = product.Increment(product);                 product.Show(); //-----------------------------------------------------------------------------------                 //decrements sum by 1, then displays                 cout << "The sum subtracted by 1 equals ";                 sum = sum.Decrement(sum);                 sum.Show();                                 cout << endl;                 cout << "Would you like to enter another set of fractions? y or n? " << endl;                 cin >> ans;                 cout << endl << endl;         }while(ans == 'y' || ans == 'Y'); return 0; }```

This is the .h file:

Code:

``` class Fraction {         friend Fraction operator + (Fraction, Fraction);         friend Fraction operator * (Fraction, Fraction);                 public:           Fraction(int n, int d = 1);            //constructor           Fraction();                                      //Set numerator = 0, denominator = 1                   void Get();                                      //Get a fraction from keyboard           void Show();                              //Display a fraction on screen           double Evaluate();                    //Return the decimal value of a fraction                 Fraction Increment(Fraction r);         Fraction Decrement(Fraction r);         Fraction Reduce(Fraction r);         private:                           int numerator;           int denominator; }; Fraction operator + (Fraction f1, Fraction f2) {   Fraction r;                                //the return value of f1 + f2                                                   //compute numerator   r.numerator = (f1.numerator * f2.denominator) + (f2.numerator * f1.denominator);                                                   //compute denominator   r.denominator = f1.denominator * f2.denominator;   return r; } Fraction operator * (Fraction f1, Fraction f2) {         Fraction r;         r.numerator = (f1.numerator * f2.numerator);         r.denominator = (f1.denominator * f2.denominator);         return r; } Fraction::Fraction(int n, int d) {         numerator = n;     denominator = d; } Fraction::Fraction() {         numerator = 0;     denominator = 1; } void Fraction::Get() {   char div_sign;                      //consumes '/' character   cin >> numerator >> div_sign >> denominator; } void Fraction::Show()  {   cout << numerator << '/' << denominator << endl; } double Fraction::Evaluate()  {   double n = numerator;                      //convert numerator to float   double d = denominator;                    //convert denominator to float   return (n / d);                                  //return float representation } Fraction Fraction::Increment(Fraction r) {         r.numerator = r.numerator + r.denominator;         return r; }        Fraction Fraction::Decrement(Fraction r) {         r.numerator = r.numerator - r.denominator;         return r; } Fraction Fraction::Reduce(Fraction r) {         int remainder;                 if (r.denominator > r.numerator)         {                        remainder = r.denominator % r.numerator;                 if (remainder != 0)                 {                         r.denominator = r.denominator / remainder;                         r.numerator = r.numerator / remainder;                         return r;                 }                 else if (remainder == 0)                 {                         r.denominator = r.denominator / r.numerator;                         r.numerator = r.numerator / r.numerator;                                                 return r;                 }         }                        if (r.numerator > r.denominator)         {                 remainder = r.numerator % r.denominator;                 if (remainder != 0)                 {                         r.numerator = r.numerator / remainder;                         r.denominator = r.denominator / remainder;                         return r;                 }                 else if (remainder == 0)                 {                                r = r.numerator / r.denominator;                         return r;                 }         }        }```
• 11-12-2002
master5001
Aside from the syntax errors *grumble grumble* I got the correct answers. Could you tell me what you where doing that was returning wrong results?

Okay, nevermind it is broken.
[/edit]
• 11-12-2002
beege31337
Quote:

The program works but i am getting the wrong output.
I like that line ;)
• 11-12-2002
4point5
I'd like to help, but try and isolate the specific code thats giving you a problem and post just that.
• 11-13-2002
ammar
I didn't read the code, but I from the subject of the thread, I can tell that you are not able to reduce the fraction?
if it's so, you don't know how - I mean the algorithm - or your code isn't working, if so post the function you are using.

The idea is that you have to get the GCD Greatest Common Devisor, then devide both the numerator and denominator by it.