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

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...
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...
Define a class named University with data members such as campus_name, courses_offered and no_of_faculty. Include appropriate...
Define a class named University with data members such as campus_name, courses_offered and no_of_faculty. Include appropriate methods and illustrate the following object oriented concepts. i. Polymorphism. ii. Static methods
Define a class named University with data members such as campus_name, courses_offered and no_of_faculty. Include appropriate...
Define a class named University with data members such as campus_name, courses_offered and no_of_faculty. Include appropriate methods and illustrate the following object oriented concepts. i. Polymorphism. ii. Static methods
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size 3x3 as private member.        There should be two public methods within the class definition namely: void setMatrixValue(int i, int j); that should set m[i][j] with user defined values int getMatrixValue(int i, int j); that should return m[i][j] Make a global function named ‘CrossProduct(Matrix m1, Matrix m2)’ that should compute the marix multiplication
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT