Expand upon selectionSortType so that you are no longer boun

Expand upon selectionSortType so that you are no longer bound to a list of 1000 elements. Thus, you\'ll be using pointers to create dynamic arrays.

template <class elemType>
void selectionSortType<elemType>::setUnsortedList (const elemType list[], int size)
{
elementsInList = size;
for (int i=0; i<size; i++) {
    unsortedList[i] = list[i];
}
sortList();
}

template <class elemType>
void selectionSortType<elemType>::getUnsortedList(elemType list[], int& size) const
{
size = elementsInList;
for (int i=0; i<elementsInList; i++) {
    list[i] = unsortedList[i];
}
}

template <class elemType>
void selectionSortType<elemType>::getSortedList(elemType list[], int& size) const
{
size = elementsInList;
for (int i=0; i<elementsInList; i++) {
    list[i] = sortedList[i];
}
}

template <class elemType>
int selectionSortType<elemType>::getLargestElement() const
{
return sortedList[elementsInList-1];
}

template <class elemType>
int selectionSortType<elemType>::getSmallestElement() const
{
return sortedList[0];
}

template <class elemType>
float selectionSortType<elemType>::getAverage() const
{
float sum = 0;
for (int i=0; i<elementsInList; i++) {
    sum += sortedList[i];
}
return sum/elementsInList;
}

template <class elemType>
void selectionSortType<elemType>::printUnsortedList(std::ostream& output) const
{
for (int i=0; i<elementsInList; i++) {
    output << unsortedList[i] << \" \";
}
output << std::endl;
}

template <class elemType>
void selectionSortType<elemType>::printSortedList(std::ostream& output) const
{
for (int i=0; i<elementsInList; i++) {
    output << sortedList[i] << \" \";
}
output << std::endl;
}

template <class elemType>
bool selectionSortType<elemType>::equalList(const selectionSortType& otherList) const
{
if (otherList.elementsInList != elementsInList)
    return false;
for (int i=0; i<elementsInList; i++) {
    if (otherList.sortedList[i] != sortedList[i])
      return false;
}
return true;
}

template <class elemType>
selectionSortType<elemType>::selectionSortType(const elemType list[], int size)
{
setUnsortedList(list,size);
}

template <class elemType>
selectionSortType<elemType>::selectionSortType() //default constructor
{
elementsInList = 0;
}

template <class elemType>
void selectionSortType<elemType>::sortList()
{

for (int i=0; i<elementsInList; i++) {
    sortedList[i] = unsortedList[i];
}

int currentPosition;
int smallestIndex;

elemType smallestElement;
int location;

for (currentPosition = 0; currentPosition < elementsInList - 1; currentPosition++) {
     
      smallestIndex = currentPosition;

      for (location = currentPosition + 1; location < elementsInList; location++)
         if (sortedList[location] < sortedList[smallestIndex])
            smallestIndex = location;
      
      smallestElement = sortedList[smallestIndex];
      sortedList[smallestIndex] = sortedList[currentPosition];
      sortedList[currentPosition] = smallestElement;

}
}

Solution

template <class elemType>
void selectionSortType<elemType>::setUnsortedList (const elemType list[], int size)
{
elementsInList = size;
for (int i=0; i<size; i++) {
    unsortedList[i] = list[i];
}
sortList();
}

template <class elemType>
void selectionSortType<elemType>::getUnsortedList(elemType list[], int& size) const
{
size = elementsInList;
for (int i=0; i<elementsInList; i++) {
    list[i] = unsortedList[i];
}
}

template <class elemType>
void selectionSortType<elemType>::getSortedList(elemType list[], int& size) const
{
size = elementsInList;
for (int i=0; i<elementsInList; i++) {
    list[i] = sortedList[i];
}
}

template <class elemType>
int selectionSortType<elemType>::getLargestElement() const
{
return sortedList[elementsInList-1];
}

template <class elemType>
int selectionSortType<elemType>::getSmallestElement() const
{
return sortedList[0];
}

template <class elemType>
float selectionSortType<elemType>::getAverage() const
{
float sum = 0;
for (int i=0; i<elementsInList; i++) {
    sum += sortedList[i];
}
return sum/elementsInList;
}

template <class elemType>
void selectionSortType<elemType>::printUnsortedList(std::ostream& output) const
{
for (int i=0; i<elementsInList; i++) {
    output << unsortedList[i] << \" \";
}
output << std::endl;
}

template <class elemType>
void selectionSortType<elemType>::printSortedList(std::ostream& output) const
{
for (int i=0; i<elementsInList; i++) {
    output << sortedList[i] << \" \";
}
output << std::endl;
}

template <class elemType>
bool selectionSortType<elemType>::equalList(const selectionSortType& otherList) const
{
if (otherList.elementsInList != elementsInList)
    return false;
for (int i=0; i<elementsInList; i++) {
    if (otherList.sortedList[i] != sortedList[i])
      return false;
}
return true;
}

template <class elemType>
selectionSortType<elemType>::selectionSortType(const elemType list[], int size)
{
setUnsortedList(list,size);
}

template <class elemType>
selectionSortType<elemType>::selectionSortType() //default constructor
{
elementsInList = 0;
}

template <class elemType>
void selectionSortType<elemType>::sortList()
{

for (int i=0; i<elementsInList; i++) {
    sortedList[i] = unsortedList[i];
}

int currentPosition;
int smallestIndex;

elemType smallestElement;
int location;

for (currentPosition = 0; currentPosition < elementsInList - 1; currentPosition++) {
      
      smallestIndex = currentPosition;

      for (location = currentPosition + 1; location < elementsInList; location++)
         if (sortedList[location] < sortedList[smallestIndex])
            smallestIndex = location;
       
      smallestElement = sortedList[smallestIndex];
      sortedList[smallestIndex] = sortedList[currentPosition];
      sortedList[currentPosition] = smallestElement;

}
}

Expand upon selectionSortType so that you are no longer bound to a list of 1000 elements. Thus, you\'ll be using pointers to create dynamic arrays. template <
Expand upon selectionSortType so that you are no longer bound to a list of 1000 elements. Thus, you\'ll be using pointers to create dynamic arrays. template <
Expand upon selectionSortType so that you are no longer bound to a list of 1000 elements. Thus, you\'ll be using pointers to create dynamic arrays. template <
Expand upon selectionSortType so that you are no longer bound to a list of 1000 elements. Thus, you\'ll be using pointers to create dynamic arrays. template <

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site