Rational Numbers Assignment Adding

This is a discussion on Rational Numbers Assignment Adding within the C++ Programming forums, part of the General Programming Boards category; We have everything done except we do not know how to do the adding of rational numbers. can you help? ...

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

    Rational Numbers Assignment Adding

    We have everything done except we do not know how to do the adding of rational numbers. can you help?

    Code:
    #include "Rational.h"
    #include "math.h"
    #include <iostream>
    
    using namespace std;
    
    Rational::Rational()
    {
         numerator = denominator = 1;
    }
    
    Rational::Rational(int N, int D)
    {
        numerator = N;
        denominator = D;
    } 
    
    int Rational::getN()
    {
                  return numerator;
    }
    
    int Rational::getD()
    {
                  return denominator;
    }
    
    int Rational::multiply(Rational rational2)
    {
        int finalN =getN()*rational2.getN();
        int finalD =getD()*rational2.getD();
        Rational rational(finalN,finalD);
        simplify(rational);
    } 
    
    int Rational::divide(Rational rational2)
    {
        int finalN =getN()*rational2.getD();
        int finalD =getD()*rational2.getN();
        Rational rational(finalN,finalD);
        simplify(rational);
    }
    
    int Rational::add(Rational rational2)
    {
    int tempN = getN()*rational2.getD();
    int tempD = getD()*rational2.getN();
    int finalN = tempN*tempD 
    int finalD = 
    Rational rational(finalN,finalD);
    simplify(rational);
    }
    
    void Rational::simplify(Rational rational)
    {
         int x=rational.getN();
         int y=rational.getD();
         int z;
         while (y!=0)
         {
               z=x%y;
               x=y;
               y=z;
         }
         int finalN=rational.getN()/x;
         int finalD=rational.getD()/x;
         Rational rationalnew (finalN,finalD);
         cout <<finalN<<"/"<<finalD<<endl;
    }
         
            
    bool Rational::compare(Rational rational2)
    {
         float initial=(float)getN()/getD();
         float final= (float)rational2.getN()/rational2.getD();
         if (initial==final) return true;
         else return false;
    }

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    Simply put, multiply the nominators by the cross denominator and get a common denominator by multiplying the denominators together.

    Adding and Subtracting Rational Functions - Free Math Help

    An easy way to find a common denominator is by multiplying the denominators together:

    Code:
    1/4 + 3/9
    9/36 + 12/36 // 1 times 9 = 9. 3 times 4 = 12. 4 times 9 equal 36
    Knowing this, you should be able to find or make a function that does this for you.
    If you need more help, just reply and I'll write some code.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Code:
    int Rational::multiply(Rational rational2)
    {
        int finalN =getN()*rational2.getN();
        int finalD =getD()*rational2.getD();
        Rational rational(finalN,finalD);
        simplify(rational);
    }
    Why not make simplify() a function that operates on "this" rational? That makes more sense to me. Unless the interface is part of the assignment that you can't change, of course.

    Code:
    if (initial==final) return true;
    This is dangerous because
    1. the difference can be smaller than representable in a float
    2. floats (and doubles) should generally not be compared with == because not all values can be represented with infinite precision

    For this function, I would just simplify both functions, and see if the denominators and numerators are equal.

    Also, for simplify(), I would "float" the negative sign to the top, if there is one. So "1/(-4) == (-1)/4" will be true.

    If you need more help, just reply and I'll write some code.
    Just in case... please don't write people's homework for them.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by cyberfish View Post
    For this function, I would just simplify both functions, and see if the denominators and numerators are equal.
    I assume you mean "fraction" not "function." I would not simplify them. I'd just check if numer1 * denom2 == numer2 * denom1.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Yes (to both) of course .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Times function for rational numbers
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2009, 04:09 PM
  2. Adding up all the numbers in the list...
    By Grayson_Peddie in forum C# Programming
    Replies: 0
    Last Post: 06-04-2003, 11:57 AM
  3. How to deal with repeating rational numbers
    By carrja99 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 11:33 AM
  4. For/Next Loops...adding 10 numbers...
    By IanelarAzure in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2002, 12:02 PM
  5. adding odd numbers
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 09-06-2001, 01:44 PM

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