Putting rational objects into an Array
Mar 4, 2013 at 12:41am UTC
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include "Rational.h"
#include "Random.h"
#include <iostream>
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#define RATIONAL_H
#include <iostream>
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "Rational.h"
#include <ostream>
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 Mar 4, 2013 at 1:07am UTC
Mar 4, 2013 at 1:18am UTC
okay I've changed this:
1 2 3 4 5
Numerator = cRandom.getNumber (MIN_NUMERATOR, MAX_NUMERATOR);
cRational[i].mNumerator = Numerator;
Denominator = cRandom.getNumber (MIN_DENOMINATOR, MAX_DENOMINATOR);
cRational[i].mDenominator = Denominator;
To this:
1 2
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.