Like Tree1Likes
  • 1 Post By iMalc

Issue finding greater fraction

This is a discussion on Issue finding greater fraction within the C++ Programming forums, part of the General Programming Boards category; I had this function working before, until my teacher told me to "make it more flexible", in which I pretty ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    12

    Issue finding greater fraction

    I had this function working before, until my teacher told me to "make it more flexible", in which I pretty much revamped the project and couldn't fix the function. My problem is that the ints great1 and great2 output their values as rounded to the lower number, which makes the if statement always false (=bool equality). However, the code then reads the bool equality as true, which is not always the correct answer.

    Can anyone help? This would help me solve two other functions.

    FractionsClass.h:
    Code:
    #include<string>
    using namespace std;
    #ifndef _fractionsclass_
    #define _fractionsclass_
    
    
    class FractionsClass
    {
    public:
        //constructor
        FractionsClass();
        FractionsClass(int);
        //destructor
        ~FractionsClass();
        //functions
        void addFrac(); //add two fractions
        void subtractFrac(); //subtract two fractions
        void multFrac(); //multiply fractions
        void divideFrac(); //divide fractions
        void reduce(); //reduce fraction
        void reducesingle();
        bool isGreaterThanFrac(bool,bool); //is one fraction greater than another
        bool isLessThanFrac(bool,bool); //is one fraction less than another
        bool equalFrac(bool); //check to see if fractions are equivalent
        void swap();
        //accessors
        void setFract(int,int); //set fraction
        void changeNum1(int);
        void changeDen1(int);
        void changeNum2(int);
        void changeDen2(int);
        void changeNewNum(int);
        void changeNewDen(int);
        int changeEquality(bool,bool);
        int returnNum1();
        int returnDen1();
        int returnNum2();
        int returnDen2();
        int returnNewNum();
        int returnNewDen();
    
    
    private:
        int num1, den1, num2, den2, n1, d1, n2, d2, newnum, newden, cn1, cd1, cn2, cd2, cnum1, cnum2, mixnum1, holdwhole1, mixnum2, holdwhole2, holdwhole3, newwhole, enter, change, rem1, rem2, newnum2;
    };
    
    
    #endif
    FractionsClass.cpp:
    Code:
    #include"FractionsClass.h"
    #include<iostream>
    #include<conio.h>
    #include<stdio.h>
    #include<math.h>
    using namespace std;
    
    
    FractionsClass::~FractionsClass()
    {
        //getch;
    }
    
    
    FractionsClass::FractionsClass()
    {
        num1=0;
        den1=0;
        num2=0;
        den2=0;
    }
    
    
    FractionsClass::FractionsClass(int hold)
    {
        num1=n1;
        den1=d1;
        num2=n2;
        den2=d2;
    }
    
    
        ...
    
    
        bool FractionsClass::isGreaterThanFrac(bool equality, bool equality2)
        {
            int great1, great2;
            great1=returnNum1()/returnDen1();
            great2=returnNum2()/returnDen2();
            if(great1>great2)
            {
                equality=true;
            }
            else
            {
                equality=false;
                if(great1==great2)
                {
                    equality2=true;
                }
                else
                {
                    equality2=false;
                }
            }
            changeEquality(equality,equality2);
            return equality;
            return equality2;
        }
    
    
        ...
    
    
        void FractionsClass::changeNum1(int n1)
        {
            num1=n1;
        }
    
    
        void FractionsClass::changeDen1(int d1)
        {
            den1=d1;
        }
    
    
        void FractionsClass::changeNum2(int n2)
        {
            num2=n2;
        }
    
    
        void FractionsClass::changeDen2(int d2)
        {
            den2=d2;
        }
    
    
        void FractionsClass::changeNewNum(int newnum)
        {
            newnum=newnum;
        }
    
    
        void FractionsClass::changeNewDen(int newden)
        {
            newden=newden;
        }
    
    
        int FractionsClass::changeEquality(bool equality, bool equality2)
        {
            equality=equality;
            equality2=equality2;
            return equality;
            return equality2;
        }
    
    
        int FractionsClass::returnNum1()
        {
            return num1;
        }
    
    
        int FractionsClass::returnDen1()
        {
            return den1;
        }
    
    
        int FractionsClass::returnNum2()
        {
            return num2;
        }
    
    
        int FractionsClass::returnDen2()
        {
            return den2;
        }
    
    
        int FractionsClass::returnNewNum()
        {
            return newnum;
        }
    
    
        int FractionsClass::returnNewDen()
        {
            return newden;
        }
    ClassDriver.cpp
    Code:
    #include"FractionsClass.h"
    #include<iostream>
    #include<conio.h>
    #include<stdio.h>
    #include<math.h>
    using namespace std;
    
    
    int main()
    {
        FractionsClass hold;
        bool equality=true, equality2=true;
        int a=0, tellme=0, run=0;
        int fraction1[2], fraction2[2];
        int n1, d1, n2, d2, num1, den1, num2, den2, newnum, newden;
        FractionsClass FractionsClass;
        while(hold.returnDen1()==0)
        {
            system("cls");
            cout<<"Enter fractions in format ( numerator / denominator ) in improper form"<<endl<<endl;
            cout<<"Input first number:  ";
            scanf("%i/%i",&fraction1[0],&fraction1[1]);
            n1=fraction1[0];
            hold.changeNum1(n1);
            d1=fraction1[1];
            hold.changeDen1(d1);
            if(hold.returnDen1()==0)
            {
                cout<<"   Illegal fraction: divide by 0"<<endl<<"   Press enter to reinput"<<endl;
                getch();
                continue;
            }
            else
            {
                while(hold.returnDen2()==0)
                {
                    system("cls");
                    cout<<"Enter fractions in format ( numerator / denominator ) in improper form"<<endl<<endl;
                    cout<<"Input first number:  "<<hold.returnNum1()<<"/"<<hold.returnDen1()<<endl;
                    cout<<"Input second number: ";
                    scanf("%i/%i",&fraction2[0],&fraction2[1]);
                    n2=fraction2[0];
                    hold.changeNum2(n2);
                    d2=fraction2[1];
                    hold.changeDen2(d2);
                    if(hold.returnDen2()==0)
                    {
                        cout<<"   Illegal fraction: divide by 0"<<endl<<"   Press enter to reinput"<<endl;
                        getch();
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        while(a<10000)
        {
            system("cls");
            cout<<"First number:  "<<hold.returnNum1()<<"/"<<hold.returnDen1()<<endl;
            cout<<"Second number: "<<hold.returnNum2()<<"/"<<hold.returnDen2()<<endl;
            //hold.showFrac();
            //hold.showShow();
            cout<<endl<<"Choose a function:"<<endl;
            cout<<"1. Find a sum of fractions"<<endl;
            cout<<"2. Find a difference of fractions  (first-second)"<<endl;
            cout<<"3. Find a product of fractions"<<endl;
            cout<<"4. Find a quotient of fractions    (first/second)"<<endl;
            cout<<"5. Reduce the fractions"<<endl;
            //cout<<"6. Make the fractions mixed"<<endl;
            //cout<<"7. Make the fractions improper"<<endl;
            cout<<"6. Find a greater fraction"<<endl;
            cout<<"7. Find a lesser fraction"<<endl;
            cout<<"8. Check if fractions are equal to each other"<<endl;
            cout<<endl<<"9. Swap the values of the fractions"<<endl;
            cout<<"10. Change the current values"<<endl;
            //cout<<"13. Change the results view"<<endl;
            cout<<endl<<"Enter any other value to exit"<<endl;
            cin>>tellme;
            system("cls");
            ...
            else if(tellme==6)
            {
                //hold.showFrac();
                //hold.tempmakeImproper();
                hold.isGreaterThanFrac(equality,equality2);
                hold.returnNum1();
                hold.returnDen1();
                hold.returnNum2();
                hold.returnDen2();
                if(equality=true)
                {
                    cout<<fraction1[0]<<"/"<<fraction1[1]<<" is greater than "<<fraction2[0]<<"/"<<fraction2[1]<<endl;
                }
                else if(equality=false)
                {
                    if(equality2=true)
                    {
                        cout<<fraction1[0]<<"/"<<fraction1[1]<<" is equal to "<<fraction2[0]<<"/"<<fraction2[1]<<endl;
                    }
                    else if(equality2=false)
                    {
                        cout<<fraction2[0]<<"/"<<fraction2[1]<<" is greater than "<<fraction1[0]<<"/"<<fraction1[1]<<endl;
                    }
                }
            }
            ...
            else
            {
                return;
            }
            cout<<endl<<"Would you like to run another function?"<<endl;
            cout<<"1. Yes"<<endl;
            cout<<"2. No"<<endl;
            cin>>run;
            if(run==1)
            {
                a++;
                continue;
            }
            else if(run==2)
            {
                return;
            }
        }
    return 0;
    }

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Are you required to use integers for great1 and great2?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    You've kinda missed the point of OOP. A fraction class should contain 2 member variables, not 27!
    A class should do one thing and do it well.

    An add operation for example, should take two instances of the class and produce a third.

    Edit:
    You cannot execute two return statments one after another to return two things.
    Also, I would use integers for this. To compare fractions, instead of division the numerators by their denominator, multiply the other side by the denominator.
    I.e.
    Code:
    1   2
    - < - <=> 1 * 3 < 2 * 2
    2   3
    This way, no floating point is required at all, and you won't get any inaccuracies because of it. Only issue is that you may have to avoid integer overflow on the multiplications.
    Last edited by iMalc; 12-07-2012 at 08:56 PM.
    Lesshardtofind likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    In addition to iMalc's comments, you can also simplify the fractions (e.g. turn 2/4 into the equivalent 1/2). That can be achieved, in general, by dividing both denominator and numerator by their greatest common divisor. For a technique to compute the greatest common divisor of the denominator and numerator, look up Euclid's algorithm. That won't eliminate the possibility of integer overflow that iMalc referred to, but does help reduce chances of it occurring.

    Given that a fraction is comparable using standard math operators (equality, greater than, etc) it wouldn't hurt to implement various operators for a Fraction class. That way you can compare two fractions using an expression "if (fract1 > frac2)" rather than being forced to use the less clear "if (frac1.GreaterThan(frac2))".
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To Fix This Issue In A Program For Finding Prime Numbers
    By Patriwths in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2012, 06:47 AM
  2. Replies: 10
    Last Post: 05-15-2011, 06:52 PM
  3. Need Help finding potential Memory issue
    By JoshNCSU22 in forum C Programming
    Replies: 9
    Last Post: 10-29-2008, 09:58 AM
  4. Replies: 18
    Last Post: 03-26-2008, 09:01 AM
  5. Greater than, less than
    By Extropian in forum C Programming
    Replies: 3
    Last Post: 06-09-2005, 11:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21