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...
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...
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...
This question is in C++ Problem 3 Write a templated C++ class to represent an itinerary....
This question is in C++ Problem 3 Write a templated C++ class to represent an itinerary. An itinerary has a title, a source position and a destination position. In addition, it may include intermediate positions. All positions have the same abstract type T. The real type can be decided later, for example it can be a cartesian point with x,y,z coordinates, or an airport code, or a city name, or a country name, etc .. The itinerary includes a vector...
C++ Define a base class called Person. The class should have two data members to hold...
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 !=...
1. Define a class counterType to implement a counter. Your class must have a private data...
1. Define a class counterType to implement a counter. Your class must have a private data member counter of type int and functions to set counter to the value specified by the user, initialize counter to 0, retrieve the value of counter, and increment and decrement counter by one. The value of counter must be nonnegative. 2. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design a class bookType that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT