Help with Rational Numbers (C++)

This is a discussion on Help with Rational Numbers (C++) within the C++ Programming forums, part of the General Programming Boards category; Hi Im completely new to programming and was wondering if anyone would be willing to help me with this exercise ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    1

    Help with Rational Numbers (C++)

    Hi

    Im completely new to programming and was wondering if anyone would be willing to help me with this exercise in C++. I have absolutely no idea how to do this exercise:
    -----------------------------------------------

    Rational.h is a C++ header file which declares a class (Rational) which handles Rational numbers (i.e. fractions).

    You must create a C++ file which contains the definitions for all the methods in that class. Rational.cc is the start of just such a file, which contains one method to get you started.
    Once you have the class methods defined, you can use it with the TestRat.cc test driver program. Using g++ you would perform the following steps:

    1. g++ -c Rational.cc //this creates Rational.o with the class definitions in it
    2. g++ -c TestRat.cc //this creates TestRat.o with the main function in it
    3. g++ Rational.o TestRat.o -o TestRat //this combines the class definitions with the class usage in the main function and creates an executable file called TestRat

    Code:
    Rational.h is given here:
    
    const int MAXB=20;
    
    class Rational {
    
    private:
    int numerator, denominator;
    char buffer[MAXB];
    
    static int gcd (int a, int b); //a helpful function that returns the
    //Greatest Common 
    Denominator of
    //two numbers
    
    void normalize(); //Make sure that this fraction is in
    //its lowest terms (ie 3/12 -> 1/4)
    //Hint: use gcd()
    
    public:
    Rational(); //default constructor of a Rational
    //number whose numerator and
    //denominator are 0/1
    Rational(int n, int d); //constructor of a Rational number
    //whose numerator and denominator
    //are n/d
    
    char *toString(); //turns this number into a string
    //using this object's handy buffer
    
    Rational plus(Rational r); // add two rational numbers
    Rational minus(Rational r); // subtract two rational numbers
    Rational times(Rational r); // multiply two rational numbers
    Rational divide(Rational r); // divide two rational numbers
    
    // don't forget that
    // a/b + c/d == (ad + bc) / bd
    // a/b - c/d == (ad - bc) / bd
    // a/b * c/d == ac / bd
    // a/b / c/d == ad / bc
    };
    
    Rational.cc is given here:
    
    #include <stdio.h>
    #include "Rational.h"
    
    int Rational::gcd (int a, int b){ 
    if (b == 0) return a; 
    else return gcd (b, a % b); 
    }
    
    TestRat.cc is given here:
    
    #include <iostream.h>
    #include "Rational.h"
    
    int main(int argc, char *argv[]){
    Rational a(2,3);
    Rational b(1,2);
    
    Rational res;
    
    res=a.plus(b);
    cout << a.toString() << " + " << b.toString() << " = " <<
    res.toString() << endl ;
    
    res=a.minus(b);
    cout << a.toString() << " - " << b.toString() << " = " <<
    res.toString() << endl ;
    
    res=a.times(b);
    cout << a.toString() << " * " << b.toString() << " = " <<
    res.toString() << endl ;
    
    res=a.divide(b);
    cout << a.toString() << " / " << b.toString() << " = " <<
    res.toString() << endl ;
    
    }
    --------------------------------------------------------------------------------
    If someone could possibly do the exercise and run my through it, i would be really grateful.

    Thanks alot

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You have all the formulas and a helpful hint for normalize, so what are you stuck with?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    IDK what ur problem is, but as far as im concerned, u cant declare a static array using a variable. U have to manually put in the number, or make it a dynamic array

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Furbiesandbeans View Post
    IDK what ur problem is, but as far as im concerned, u cant declare a static array using a variable. U have to manually put in the number, or make it a dynamic array
    Where do you see such?

    If you think this is wrong, I can tell you it isn't:
    Code:
    const int MAXB=20;
    
    class Rational {
    
    private:
    int numerator, denominator;
    char buffer[MAXB];
    MAXB is a const int, so to the compiler, that's just as good as 20 as a constant. It's not allowed to change during the runtime of the code, so the array is not "declaring an array using a variable".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rational Numbers Assignment Adding
    By CaliJoe in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2009, 11:37 PM
  2. Reducing rational numbers - code not working properly
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 07:42 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. How to deal with repeating rational numbers
    By carrja99 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 11:33 AM

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