Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++, 1.1 BUBBLE SORTING Bubble sorting is a prevailing method to sort an arra

ID: 3851745 • Letter: I

Question

IN C++, 1.1 BUBBLE SORTING Bubble sorting is a prevailing method to sort an array in either descending or ascending order. Before you start, make sure you’re able to write a small bubble-sorting function to sort an int array, which has two functions as below void swap(a, b) void bubbleSorting(int array[ ], const int& size) Try to use char, double, float array instead of int array, and test your program to make sure all work fine in descending or ascending order.

Then a natural intuition comes out that you could use template function to re-write this bubble-sort function.

1.2 CLASS DEFINITION Write a template class Holder which has a private template member variable element which might be char, double, float, int or string type. You should design member functions as below

• Default constructor

Any appropriate operation inside is encouraged, and left empty is also fine

• Constructor with one argument Pass in value of outside to element by argument

• Destructor

• Overloaded operator + = Take a Holder object as argument, and would return a newly created Holder object with summed/equal element. • Overloaded stream <<and>>

• Appropriate accessor function Besides, you should also design a non-member overloaded operator - which takes two Holder object and return a third object with difference value of element.

1.3 MAIN FUNCTION Write a main function to test bubble sorting for a Holder array. 1

. Read from command line two positive integers. First int would be the size of Holder array (size is integers from 4 to 8), and second int would be either 1 or 2 (1 means ascending order and 2 means descending order).

Should judge argc 3 or not, should judge whether input numbers are positive and integers within designated range. For example, would test ./solution abc -2, ./solution -1.3 1.456, ./solution 10.346 string, and all these should fail and exit erroneously.

. 2. Create a Holder dynamic array After creating the array, you should use overloaded stream À to enable user to define some (not all) Holder objects in the array, and rest of objects in the array should be defined by calling all different overloaded + - = operators. 3

. Call overloaded stream << to print the current array (before sorted)

. Bubble sort the Holder array by invoking your bubbleSort function, and call overloaded stream >> to print the current array (after sorted). Make sure the array is actually sorted well

Explanation / Answer

Here is the answer for the question. Please do rate the answer if it helped. Thank you very much.

#include <iostream>

using namespace std;

template <typename T>

void Swap(T &a, T &b);

template <typename T>

void BubbleSort(T array[], const int &size, bool ascending = true);

template <typename T>

void Display(T array[], const int &size);

//define the templated class Holder

template <typename T>

class Holder

{

T element;

  

public:

  

//default constructor

Holder() {}

  

//constructor with

Holder(T val)

{

element = val;

}

  

//destructor

~Holder(){}

  

  

//accessor

T getElement() const

{

return element;

}

  

//+ operator to do sum

Holder operator + (const Holder &other)

{

Holder h(element + other.element);

return h;

}

  

//assignment operator =

Holder operator = (const Holder &other)

{

element = other.element ;

return *this;

}

  

//operator > for used for comparison

bool operator > (const Holder &other)

{

return element > other.element;

}

  

//overloaded << operator

friend ostream & operator << (ostream &out, const Holder &h)

{

out << h.element ;

return out;

}

  

  

  

//overloaded >> operator

friend istream & operator >> (istream &in, Holder &h)

{

in >> h.element;

return in;

}

  

  

  

};

//non member operator - for Holder<T>

template <typename T>

Holder<T> operator - (const Holder<T> &h1, const Holder<T> &h2)

{

Holder<T> h(h1.getElement() - h2.getElement());

return h;

}

int main(int argc, char *argv[])

{

//check if correct number of arugments are passed

if(argc != 3)

{

cout << "Need 2 arguements !" << endl;

cout << "Usage: " << argv[0] << " <size> <sort_order>" << endl;

  

return -1;

}

  

int size = atoi (argv[1]), sort = atoi(argv[2]);

if(size <4 || size > 8)

{

cout << "size should be an int value in the range 4 - 8" <<endl;

return -1;

}

  

if(sort != 1 && sort != 2)

{

cout << "sort order should be either 1(ascending) or 2(descending)" << endl;

return -1;

}

  

//allocate memory for an Holder<int> array

Holder<int> *holderArr = new Holder<int>[size];

  

//get all but 3 elements input from user. The last 3 elemnts are filled using operators defined

for(int i = 0; i < size - 3; i++)

{

cout << "Enter an holder value (int): " ;

cin >> holderArr[i];

}

  

//fill the rest of array elements using the operators defined

  

holderArr[size - 3] = holderArr[0]; //using assignment operator

holderArr[size - 2] = holderArr[0] + holderArr[1]; //using member + operator

holderArr[size - 1 ] = holderArr[0] - holderArr[1]; //using non-member - operator

  

  

cout << "Before sorting the holder array contents " << endl;

Display(holderArr, size);

  

BubbleSort(holderArr, size, sort == 1);

  

cout << "After sorting the holder array contents " << endl;

Display(holderArr, size);

  

return 0;

}

template <typename T>

void Swap(T &a, T &b)

{

T temp;

temp = a;

a = b;

b = temp;

  

}

template <typename T>

void BubbleSort(T array[], const int &size , bool ascending)

{

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

{

for(int j = 0; j < size - i; j++)

{

if((ascending && array[j] > array[j+1]) || (!ascending && array[j+1] > array[j]))

Swap(array[j], array[j+1]);

}

}

}

template <typename T>

void Display(T array[], const int &size)

{

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

cout << array[i] << endl;

}

output

/a.out 5 2
Enter an holder value (int): 5
Enter an holder value (int): 7
Before sorting the holder array contents
5
7
5
12
-2
After sorting the holder array contents
12
7
5
5
-2