Question

In: Computer Science

Write a program in which define a templated class mySort with private data members as a...

Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.

Solutions

Expert Solution

In case of any query do comment. Please rate answer. Thanks

Note: There was nothing mentioned for the usage of counter. If you need anything further do let me know.

#include <iostream>

using namespace std;

template <typename T>

//class my sort

class mySort {

private:

    int counter;

              T *array; //array to hold elements

              int numberOfElements;

public:

             

              mySort(T arr[], int numberOfElements);

              void sortAlgorithm();

              void mySwap(T&x,T&y);

              void displayData();

};

//constructor

template <typename T>

mySort<T>::mySort(T arr[], int number) {

             

              array = new T[number];

              numberOfElements = number;

              counter =0;

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

                             array[i] = arr[i];

}

//myswap method implementation

template <typename T>

void mySort<T>::mySwap(T&x,T&y)

{

    T temp=x;

    x=y;

    y=temp;

}

//sortAlgorithm implementation

template <typename T>

void mySort<T>::sortAlgorithm()

{

    for (int i = 0; i < numberOfElements - 1; i++)

        for (int j = numberOfElements - 1; i < j; j--)

            if (array[j] < array[j - 1])

              mySwap(array[j], array[j - 1]);

}

//method to display data

template <typename T>

void mySort<T>::displayData() {

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

                             cout<<" "<<array[i];

              cout<<endl;

}

int main() {

              //test with integer data

              int arrInt[5] = {12, 7, 5, 9, 8};

              mySort<int> aInt(arrInt, 5);

              aInt.sortAlgorithm();

              aInt.displayData();

             

              //test with float data

              float arrFloat[5] = {25.5, 32.3, 15.0, 11.2, 9.0};

              mySort<float> afloat(arrFloat, 5);

              afloat.sortAlgorithm();

              afloat.displayData();

             

              //test with integer data

              char arrChar[5] = {'z', 'v', 'a', 'r', 'm'};

              mySort<char> aChar(arrChar, 5);

              aChar.sortAlgorithm();

              aChar.displayData();

             

             

              return 0;

}

=======screen shot of the code=========

Output:


Related Solutions

Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called basic_stats that takes as a parameter a list of Person objects and returns a tuple containing the mean, median, and mode of all the ages. To do this,...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
Part 1 Create a class named Room which has two private data members which are doubles...
Part 1 Create a class named Room which has two private data members which are doubles named length and width. The class has five functions: a constructor which sets the length and width, a default constructor which sets the length to 12 and the width to 14, an output function, a function to calculate the area of the room and a function to calculate the parameter. Also include a friend function which adds two objects of the room class. Part...
Write a menu driven program in Java (OOP) That contain class for Botique. Data members include...
Write a menu driven program in Java (OOP) That contain class for Botique. Data members include code ,colour , size ,Quantity Your class should contains app accessors and mutators method Your class should contain Parametric and non-parametric constructors (use constructor chaining) Your class should have input and display method.
Please solve in C++ only class is not templated You need to write a class called...
Please solve in C++ only class is not templated You need to write a class called LinkedList that implements the following List operations: public void add(int index, Object item); // adds an item to the list at the given index, that index may be at start, end or after or before the // specific element 2.public void remove(int index); // removes the item from the list that has the given index 3.public void remove(Object item); // finds the item from...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT