### Putting rational objects into an Array

Alright so I'm working on an assignment and I ran into a snag.

In the program, I'm supposed to create two files and link them, one called Rational and one called Random. The Random one basically creates a pseudo-random number between an upper and lower inclusive boundary and returns it (which I'm not having a problem with). The Rational file creates an array of Rational objects which is where I'm running into a problem. In the driver, I'm trying to create and assign Rationals into an array and then print them through a for loop, but it's not allowing me to assign the Numerator and Denominator of the Rational in an array element.

Hopefully looking at my code will help, here's the RationalDrive.cpp:

 ``123456789101112131415161718192021222324252627282930`` ``````#include "Rational.h" #include "Random.h" #include using namespace std; const int MAX_RATIONALS = 10; const int MAX_NUMERATOR = 10; const int MIN_NUMERATOR = 0; const int MAX_DENOMINATOR = 10; const int MIN_DENOMINATOR = 1; int main () { Rational cRational [MAX_RATIONALS]; Rational object; Random cRandom; int Numerator = 0; int Denominator = 0; for (int i = 0; i < MAX_RATIONALS; ++i) { Numerator = cRandom.getNumber (MIN_NUMERATOR, MAX_NUMERATOR); cRational[i].mNumerator = Numerator; Denominator = cRandom.getNumber (MIN_DENOMINATOR, MAX_DENOMINATOR); cRational[i].mDenominator = Denominator; cout << "Rational Object #" << i + 1 << ": "; cRational[i].print (cout); } }``````

here's the Rational.h file:

 ``1234567891011121314151617181920212223242526`` ``````#define RATIONAL_H #include using namespace std; class Rational { public: Rational (int = 0, int = 1); void setNumerator (int); void setDenominator (int); int getNumerator (int) const; int getDenominator (int) const; void print (ostream &) const; bool bIsEqual (const Rational &) const; Rational multiply (const Rational &) const; void reduce (); private: int mNumerator; int mDenominator; int gcd (int, int); }; #endif ``````

and the class implementation:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include "Rational.h" #include using namespace std; Rational::Rational (int Numerator, int Denominator) { mNumerator = Numerator; mDenominator = Denominator; } void Rational::setNumerator (int Numerator) { mNumerator = Numerator; } void Rational::setDenominator (int Denominator) { mDenominator = Denominator; } int Rational::getNumerator (int Numerator) const { return mNumerator; } int Rational::getDenominator (int Denominator) const { return mDenominator; } void Rational::print (ostream &output) const { output << mNumerator << "/" << mDenominator << endl; } bool Rational::bIsEqual (const Rational &object) const { if (mNumerator == object.mNumerator && mDenominator == object.mDenominator) { return true; } else { return false; } } Rational Rational::multiply (const Rational &object) const { Rational cRational; int mNewNumerator; int mNewDenominator; mNewNumerator = mNumerator * object.mNumerator; mNewDenominator = mDenominator * object.mDenominator; cRational.mNumerator = mNewNumerator; cRational.mDenominator = mNewDenominator; return cRational; } void Rational::reduce () { }``````

I know I haven't written the reduce implementation yet, but I haven't used it either so I don't think that should be a problem.

Any help is extremely appreciated!!
Last edited on
okay I've changed this:
 ``12345`` `````` Numerator = cRandom.getNumber (MIN_NUMERATOR, MAX_NUMERATOR); cRational[i].mNumerator = Numerator; Denominator = cRandom.getNumber (MIN_DENOMINATOR, MAX_DENOMINATOR); cRational[i].mDenominator = Denominator;``````

To this:
 ``12`` `````` cRandom.getNumber (MIN_NUMERATOR, MAX_NUMERATOR) = cRational[i].mNumerator; cRandom.getNumber (MIN_DENOMINATOR, MAX_DENOMINATOR) = cRational[i].mDenominator;``````

In an attempt to make it shorter and possibly work, but it doesn't of course, so I'm still kind of stuck..
Topic archived. No new replies allowed.