Thread: Help with Rational Numbers (C++)

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. You have all the formulas and a helpful hint for normalize, so what are you stuck with?

3. 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. Originally Posted by Furbiesandbeans
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