Times function for rational numbers

This is a discussion on Times function for rational numbers within the C++ Programming forums, part of the General Programming Boards category; hi, i have everything working in this program expect when i made my fraction simplifying function, the multiplication of the ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    Times function for rational numbers

    hi, i have everything working in this program expect when i made my fraction simplifying function, the multiplication of the two fractions all of a sudden do not appear.

    Can you help??

    Code:
    #include "Rational.h"
    #include "math.h"
    #include <iostream>
    
    using namespace std;
    
    Rational::Rational(int N, int D)     //gets num and den
    {   numerator = N;
        denominator = D;  } 
    
    int Rational::num()                  
    {return numerator;}
    
    int Rational::den() 
    {return denominator;}
    
    int Rational::add(Rational f2)     //adds the two fractions
    {int temp = num()*f2.den();
    int temp2 = (den()*f2.num()); //num times by the den of the second and adds it to the den of the first times with the num of the second
    int fnum= temp+temp2;
    int fden = den()*f2.den();       //den of first times den of second
    Rational rational(fnum,fden);  //gets final num and den
    cout <<"Added:      ";
    simplify(rational); //simplifies the final num and den
    }
    
    int Rational::subtract(Rational f2) //subtracts the two fractions
    {int temp = num()*f2.den();
    int temp2 =den()*f2.num(); //num times second den subtracted by the den of the first and the num of the second
    int fnum= temp+temp2;
    int fden = den()*f2.den();  //den of the first and den of the second
    Rational rational(fnum,fden); //gets final num and den
    cout<< "Subtracted: ";
    simplify(rational); //simplifies the final num and den
    }
    
    int Rational::times(Rational f2) //times the two fractions
    {   int fnum =num()*f2.num();  //num of the first times the num of the second
        int fden =den()*f2.den();  //den of the first times den of the second
        Rational rational(fnum,fden); //gets the final num and den
        cout << "Timed:      ";
        simplify(rational);        //simplifies the final num and den
        }              
     
    int Rational::divide(Rational f2) //divides the two fractions
    {   int fnum =num()*f2.den();   //num times den of second
        int fden =den()*f2.num();   //den of first times num of second
        Rational rational(fnum,fden); //gets final num and den
        cout << "Divided:    ";
        simplify(rational);           //simplifies the final num and den
    }
    
    
    void Rational::simplify(Rational rational)
    {    int num1=rational.num();                 // takes the final num and den
         int num2=rational.den();
        for(int x = 1; x <= num1; ++x){ //for loop, increments until the number is greater than the numerator of the fraction
            if(num1 % x == 0 && num2 % x == 0){ //checks if each number is divisible;)
                while((num1 % x == 0 && num2 % x == 0) && x != 1){ //Incase it isn't in lowest terms
                    int fnum =(num1 /= x); //Does the dividing
                    int fden= (num2 /= x); //This is for the denomenator
                    Rational end (fnum,fden);
                    show (end);                   //shows the final ans
                }
            }
        }
    }
    
    
     
    void Rational::show(Rational end) //shows the fractions
    {  int fnum=end.num();
       int fden=end.den();
       cout <<"**"<<fnum<<"/"<<fden<<"**" <<endl;   
       }   
    
    bool Rational::compare(Rational f2) 
    //compares the two fractions to see if they are equal
    {
         float initial=(float)num()/den();
         float final= (float)f2.num()/f2.den();
         if (initial==final) return true;
         else return false;
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Suppose that the fraction is already in lowest terms (cannot be simplified). Then there is no x that evenly divides both the numerator and the denominator, so the if condition
    (num1 % x == 0 && num2 % x == 0) is never satisfied, that block of code is not executed and the function does not display anything.

    Suppose, on the other hand, that there is such an x. Then the if condition is satisfied, your program enters the while loop, which is actually an infinite loop because within that loop, num1, num2 and x never change. In this case, the program will display the fraction resulting from dividing both the numerator and denominator by x, but will hang there because it can never exit that loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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