Question: Write a modularized program to generate a fraction multiplication table using dynamic memory allocation. Use the fraction class from previous labs. Modify class fraction to
Write a modularized program to generate a fraction multiplication table using dynamic memory allocation. Use the fraction class from previous labs. Modify class fraction to keep track of number of object currently in memory Let the user input a denominator, and then generate all combinations of two such fractions that are between 0 and 1, and multiply them together. Declare a pointer to two dimensional array of pointers. Dynamically allocate two dimensional array of pointers based on the denominator entered by a user. Create fractions dynamically. Only positive fractions. No need to reduce. Well-formatted output is required ( right align all numerical values) Heres an example of the output if the denominator is 7: Fraction multiplication table Here is my previous program #include #include using namespace std; //Fraction class class Fraction { private: int whole; int numerator; int denomerator; public: Fraction();//default constructor; Fraction(int n, int d);//two arguments' constructor Fraction(int w, int n, int d);// three arguments' constructor Fraction(const Fraction&obj);// copy constructor ~Fraction();// destructor void setwhole(int w);//set whole void setnumer(int n);//set numerator void setden(int d);// set denomerator inline int getwhole() const;// get whole inline int getnumerator() const;// get numerator inline int getdenomerator() const;// get denomerator friend istream& operator>>(istream &input, Fraction &fract);// operator >> friend ostream& operator<< (ostream& output, const Fraction& fract);// operator << Fraction operator+(const Fraction & other);// operator + Fraction operator-(const Fraction & other);// operator - Fraction operator*(const Fraction & other);// operator * Fraction operator/(const Fraction & other);// operator / bool operator==(const Fraction & other);// operator == const Fraction& operator=(const Fraction&other);// opera }; Fraction::Fraction() { whole = 0; numerator = 0; denomerator = 1; } Fraction::Fraction(int n, int d) { whole = 0; if (n < 0) numerator = 0; else numerator = n; if (d <= 0) denomerator = 1; else denomerator = d; } //three arguments' constructor Fraction::Fraction(int w, int n, int d) { if (w < 0) whole = 0; else whole = w; if (n < 0) numerator = 0; else numerator = n; if (d <= 0) denomerator = 1; else denomerator = d; } //copy constructor Fraction::Fraction(const Fraction&obj) { *this = obj; } //default destructor Fraction::~Fraction() { whole = 0; numerator = 0; denomerator = 1; } //set whole void Fraction::setwhole(int w) { if (w < 0) cout << " This is invalid value. " << endl; else whole = w; } //set numerator void Fraction::setnumer(int n) { if (n < 0) cout << " This is invalid value. " << endl; else numerator = n; } //set denomerator void Fraction::setden(int d) { if (d <= 0) cout << " This is invalid value. " << endl; else denomerator = d; } //get whole int Fraction::getwhole() const { return whole; } //get numerator int Fraction::getnumerator() const { return numerator; } //get denomerator int Fraction::getdenomerator() const { return denomerator; } //operator >> istream& operator>>(istream &input, Fraction &fract) { cout << " Warning: It should be non-negative frcation and denominator can not be zero. System will "; cout << "ask you reentry." << endl; do { cout << "Enter Fraction whole "; input >> fract.whole; cout << " Enter numerator "; input >> fract.numerator; cout << " Enter denomerator. "; input >> fract.denomerator; } while (fract.whole < 0 || fract.numerator < 0 || fract.denomerator <= 0); return input; } //operator << ostream& operator<< (ostream& output, const Fraction& fract) { return output << fract.whole << " " << fract.numerator << "/" << fract.denomerator; } //operator add Fraction Fraction::operator+(const Fraction & other) { int new_num = (whole * denomerator + numerator)*other.denomerator + (other.whole*other.denomerator + other.numerator)*denomerator; return Fraction(new_num, denomerator*other.denomerator); } //operator sub Fraction Fraction::operator-(const Fraction & other) { int new_num = (whole * denomerator + numerator)*other.denomerator - (other.whole*other.denomerator + other.numerator)*denomerator; return Fraction(new_num, denomerator*other.denomerator); } //operator mul Fraction Fraction::operator*(const Fraction & other) { int new_num = (whole * denomerator + numerator)*(other.whole*other.denomerator + other.numerator); return Fraction(new_num, denomerator*other.denomerator); } //operator division Fraction Fraction::operator/(const Fraction & other) { if (other.numerator == 0) { cout << " This is invalid divide result, it will output fraction 0 0/1. " << endl; return Fraction(0, 0, 1); } else { int new_num = (whole * denomerator + numerator)*other.denomerator; return Fraction(new_num, denomerator*(other.whole*other.denomerator + other.numerator)); } } //operator == bool Fraction::operator==(const Fraction & other) { return((whole * denomerator + numerator)*other.denomerator == (other.whole*other.denomerator + other.numerator)*denomerator) ? true : false; } //operator = const Fraction& Fraction::operator=(const Fraction&other) { whole = other.whole; numerator = other.numerator; denomerator = other.denomerator; return *this; } //main int main() { Fraction f1, f2; cout << "Enter Fraction 1 "; cin >> f1; cout << " Enter Fraction 2 "; cin >> f2; Fraction addResult = f1 + f2; cout << " Addition Result: " << addResult << endl; Fraction subResult = f1 - f2; cout << " Substraction Result: " << subResult << endl; Fraction mulResult = f1 * f2; cout << " Multiplication Result: " << mulResult << endl; Fraction divResult = f1 / f2; cout << " Divistion Result: " << divResult << endl; if (f1 == f2) cout << " Two fractions are equal. "; else cout << " Two fractions are not equal." << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
