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

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)

#include "stdafx.h"

#include

#include

#include

#include

using namespace std;

//Fraction class

class Fraction

{

private:

int whole;

int numerator;

int denomerator;

static int count;

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

void setall(int w, int n, int d);// set all variable

inline int getwhole() const;// get whole

inline int getnumerator() const;// get numerator

inline int getdenomerator() const;// get denomerator

//int getcount() const;// get count

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

};

//int count = 0;

//deafault constrcutor

Fraction::Fraction()

{

whole = 0;

numerator = 0;

denomerator = 1;

count++;

}

// two argument's constrcutor

Fraction::Fraction(int n, int d)

{

whole = 0;

setnumer(n);

setden(d);

count++;

}

//three arguments' constructor

Fraction::Fraction(int w, int n, int d)

{

setwhole(w);

setnumer(n);

setden(d);

count++;

cout << " The number of Fraction " << count << endl;

}

//copy constructor

Fraction::Fraction(const Fraction&obj)

{

*this = obj;

count++;

}

//default destructor

Fraction::~Fraction()

{

whole = 0;

numerator = 0;

denomerator = 1;

count--;

}

//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;

}

//setall

void Fraction::setall(int w, int n, int d)

{

setwhole(w);

setnumer(n);

setden(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;

}

int Fraction::count = 0;

void createFractionArray(Fraction ** ptr, int columns,int row);//create the array of fraction

void mulFractionArray(Fraction ** ptr, int columns, int row);// caclute the fraction

void printFractionArray(Fraction ** ptr, int columns, int row);// print the table

void deleteFractionArray(Fraction **ptr, int columns, int row);// delete all the pointers

//main

int main()

{

int d = 0;

cout << " INput the denominator. ";

cin >> d;

int col, row;

row = d;

col = 0;

Fraction **ptr=new Fraction *[d];

try

{

//Fraction ** ptr = new Fraction*[d];

createFractionArray(ptr, col,row);// create the array of fraction

mulFractionArray(ptr, col, row);// caclute the fraction

printFractionArray(ptr, col, row);// print the table

deleteFractionArray(ptr, col, row);//delete all the pointers

}

catch (bad_alloc&ex)

{

cout << "Unable to allocate memory: " << ex.what() << " program will now close." << endl;

exit(1);

}

return 0;

}

// create fraction pointer

void createFractionArray(Fraction ** ptr, int columns, int row)

{

int i;

for (i = 0; i < columns; i++)

ptr[i] = new Fraction[row];

for(columns=0;columns

ptr[columns] = new Fraction[row];

}

// mul Fraction array

void mulFractionArray(Fraction **ptr, int col,int row)

{

int c, r;//create columns and row

for (c = 0; c < col; c++)

for (r = 0; r < row; r++)

{

if (c == 0)

ptr[c][r].setall(0, c, r);

else if (r == 0)

ptr[c][r].setall(0, c,r );

else

ptr[c][r].setall(0, r*c, col*row);

}

}

// print fraction

void printFractionArray(Fraction ** ptr, int col,int row)

{

int r, c;

for (r = 0; r < row; r++)

cout << right << setw(2) << r << " ";

cout << endl;

for (r= 1; row

{

cout << right << setw(2) << r;

for (c = 1; c

{

cout << right << setw(4) << ptr[r][c];

}

cout << endl;

}

}

//delete pointer

void deleteFractionArray(Fraction * *ptr, int col,int row)

{

for (int i = 0; i < row; i++)

{

delete[] ptr[i];

ptr[i] = nullptr;

}

delete[] ptr;

ptr = nullptr;

}

I don't know why my program doesn't work

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!