Monday, February 26, 2018

Programming Challenge 16.10 - SortableVector Class Template

Example Files: SortableVectorClassTemplate.7z

SortableVector.h


#ifndef SORTABLE_VECTOR_H_
#define SORTABLE_VECTOR_H_

#include "SimpleVector.h"

template <class T>
class SortableVector : public SimpleVector<T>
{
    public:
        SortableVector() : SimpleVector<T>()
        { }

        SortableVector(int size) : SimpleVector<T>(size)
        { }

        SortableVector(const SortableVector &);

        // Mutator function
        void sortItems();
};

/* **********************************************************
            SortableVector<T>::SortableVector()
         - Copy Constructor
   ********************************************************** */

template <class T>
SortableVector<T>::SortableVector(const SortableVector &obj) :
                      SimpleVector<T>(obj.getArraySize())
{
    for (int count = 0; count < this->getArraySize(); count++)
    {
        this->operator[](count) = obj[count];
    }
}

/* **********************************************************
            SortableVector<T>::sortItems() : const T &
   This function uses a selection sort algorithm to sort the
    array's elements in ascending order.
   ********************************************************** */

template <class T>
void SortableVector<T>::sortItems()
{
    int startScan = 0;
    int index = 0;
    int minIndex = 0;
    T minValue{};

    for (; startScan < this->getArraySize() - 1; startScan++)
    {
        minIndex = startScan;
        minValue = this->operator[](startScan);

        for (index = startScan + 1; index < this->getArraySize(); index++)
        {
            if (this->operator[](index) < minValue)
            {
                minValue = this->operator[](index);
                minIndex = index;
            }
        }

        this->operator[](minIndex) = this->operator[](startScan);
        this->operator[](startScan) = minValue;
    }
}

#endif

SortableVectorDm.cpp


#include "SortableVector.h"

#include <cstdlib>
#include <ctime>

#include <iostream>
using std::cin;
using std::cout;

#include <string>
using std::string;

/* **********************************************************
            print() : const <T> &obj, const string
    Outputs the object's current contents to screen.
    ********************************************************** */

template <class T>
void print(const SortableVector<T> &obj, const string objName)
{
    cout << "These values are currently in your " << objName << ":\n";
    cout << obj << "\n\n";
}

/* **********************************************************
                                        main()
   ********************************************************** */

int main()
{
    const int    SIZE = 16;
    const string objNames[] = { "intTable", "doubleTable", "charTable" };

    srand(static_cast<unsigned int> (time(0)));

    SortableVector<int>    intTable(SIZE);
    SortableVector<double> doubleTable(SIZE);
    SortableVector<char>   charTable(SIZE);

    cout << "SORTABLE VECTOR DEMO\n\n";
    cout << "Now filling the arrays with random values ...\n";
    for (int count = 0; count < SIZE; count++)
    {
        intTable[count] = rand() % SIZE * 1 + (4 * 2);
        doubleTable[count] = rand() % SIZE * (count + (4 * 3.44));
        charTable[count] = rand() % SIZE - 191;
    }

    print(intTable, objNames[0]);
    print(doubleTable, objNames[1]);
    print(charTable, objNames[2]);

    cout << "Now sorting the items ...\n\n";
    intTable.sortItems();
    doubleTable.sortItems();
    charTable.sortItems();
   
    print(intTable, objNames[0]);
    print(doubleTable, objNames[1]);
    print(charTable, objNames[2]);

    cout << "Thank you for trying this program! Have a nice day!";
    cin.get();
   return 0;
}

ExampleOutput:



No comments:

Post a Comment