Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this program you are asked to convert the code to a generic templated version such that the data array is capable of storing any

In this program you are asked to convert the code to a generic templated version such that the data array is capable of storing any type of data objects. To test you program, use C++ string and then rational type of data. For your convenience, the rational class whose interface is appended at the end of the containerclass. Notice that much of the code especially in the main() function can no longer be used and you must come up with your version of solutions. It is important the you check out all the member functions of thecontainer class including the sort(), which requires you to enter a list of rational numbers (a minimum of 6 is required). Your program output must show that all member function are indeed checked directly or indirectly.

#include

#include

#include // for exit function.

#include // for generating random test data values

using namespace std;

// container class interface

class container {

public:

container();

// Postcondition: all data array elements of the container are initialized to 0 and data member count is set to -1

void insert(int);

// Precondition: container must not be full

// Postcondition: if the container is not full, insert the value passed to the function into the first available data array element

// and increment count by 1; otherwise , display "The container is full: no insertion made and program terminated!"

bool isEmpty();

// Postcondition: return true if container is empty; return false otherwise

bool isFull();

// Postcondition: return true if container is full; return false otherwise

void remove();

// Precondition: the container must not be empty

// Postcondition: if the container is not empty, remove the last element (most recently) stored into the data array of the container; // otherwise display "Container is empty; program terminated!"

void undelete();

// Postcondition: the most recently removed value is restored or undeleted

void sort();

// Precondition: container contains at least two values

// Postcondition: all values stored in the container have been sorted in ascending order

void display();

// Postcondition: displays all values currently stored the container

private:

const static int CAPACITY = 10; // specifies storage capacity of a container object, i.e., the size of the container

int data[CAPACITY]; // stores inserted integer values

int count; // (count + 1) represents how many values are currently stored in the container!

}; // this is because data member count will be used as subscript for the data array.

// Implementation of member functions

container::container()

{

count = -1;

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

data[i] = 0;

}

void container::insert(int value)

{

if (!isFull())

{

data[++count] = value; // count is used as subscript for data array elements

cout << setw(4) << value << " has been inserted in data[" << count << "]." << endl;

}

else {

cout << "Attempts to insert a value into a full container; program is terminated!";

exit (1);

}

}

void container::delete() // logical removal!

{

if (!isEmpty())

{

cout << " data[" << count-- << "] is 'logically' removed from the container!" << endl;

}

else

{

cout << "Attempts to remove a value from an empty container; program is terminated!";

exit (1);

}

}

void container::sort()

{

int pass, c, temp;

if (count >= 1) // perform sort operation only when the data array contains at least two elements

{

for (pass = 1; pass < count; pass++)

for (c = 0; c <= count - pass; c++)

if (data[c] > data[c + 1]) {

temp = data[c];

data[c] = data[c + 1];

data[c + 1] = temp;

}

}

}

void container::display()

{

cout << "The 'container' contains the following " << count + 1 << " value(s): ";

if (count == -1)

cout << "*** data array is empty!" << endl;

else

{

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

cout << data[i] << '\t';

cout << endl;

}

}

int main()

{

container c;

srand(static_cast(time(0)));

cout << "We now insert 10 random integer values into the container: ";

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

c.insert(rand() % 99 + 1);

cout << " The contents of the container are: " << endl;

c.display();

c.sort();

cout << "After sort: " << endl;

c.display();

cout << "We now remove all values in the container: ";

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

c.delete();

cout << endl;

c.display();

cout << " We now perform the 'undelete' operation three times: ";

c.undelete();

c.undelete();

c.undelete();

c.display();

return 0;

}

The interface for the rational class:

A rational number is defined as a / b, where a and b are arbitrary integers (positive or negative) and b 0. Rational numbers support arithmetic operations including add, subtract, multiply, and divide. One can alsocompare the magnitude of two rational numbers. More detailed discussion of rational numbers can be found at in an article posted at https://en.wikipedia.org/wiki/Rational_number.

The interface for the rational class and the prototypes of the four non-member functions are provided below. Store them in a file named rational.h.

class rational

{

friend istream& operator>>(istream &in, rational &r);

// The user is prompted to enter two integer values for the numerator and denominator of a rational number

// Postcondition: the calling rational object is assigned with values entered by the user

friend ostream& operator<<(ostream &out, rational &r);

// Postcondition: The rational object referenced by r is display as a / b, e.g., 1 / 2, -5 /9 (not 5 / -9), 1 / 4 (not 2 / 8, etc.).

public:

rational();

// Postcondition: declared rational number is initialized to 0 / 1.

rational(int aa, int bb);

// Postcondition: declared rational number is initialized to aa / bb.

void set(int aa, int bb);

// Postcondition: the calling rational object is set to aa / bb.

rational operator+(const rational &r2) const;

// Postcondition: returns the sum of the calling rational object and r2

rational operator-(const rational &r2) const;

// Postcondition: returns the difference of subtracting r2 from the calling rational object.

rational operator*(const rational &r2) const;

// Postcondition: returns the product of the calling rational object and r2.

rational operator/(const rational &r2) const;

// Postcondition: returns the quotient of dividing the calling rational object by r2.

int operator>(const rational&r2) const;

// Postcondition: returns 1 if the calling object is greater than r2; 0 if it is equal to r2; -1 is it is less than r2

private:

int GCD() const;

// A helper member function. You must use the Euclidean algorithm. https://en.wikipedia.org/wiki/Euclidean_algorithm

// Postcondition: returns the "greatest common divisor" between the numerator and denominator of the calling rational object

int a; // numerator part of a rational number a / b

int b; // denominator part of a rational number a / b

};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial And Temporal Databases 11th International Symposium Sstd 2009 Aalborg Denmark July 8 10 2009 Proceedings Lncs 5644

Authors: Nikos Mamoulis ,Thomas Seidl ,Kristian Torp ,Ira Assent

2009th Edition

3642029817, 978-3642029813

More Books

Students also viewed these Databases questions

Question

Does it avoid using personal pronouns (such as I and me)?

Answered: 1 week ago

Question

Does it clearly identify what you have done and accomplished?

Answered: 1 week ago