SortableVector Class Template

Good morning,
I was asked to make a child class called SortableVector attached with the SimpleVector.
I am asked to making a sorting algorithm and test it with two data types.
Now the issue I am having is that it does not sort any of the values but it leaves it blank.
I am wondering what I did wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>
#include <list>
#include "SimpleVector.h"

// The # of elements in the array
const int SIZE = 8;

// This function will swap the numbers
template <class T>
void numSwap(T &outVal1, T &outVal2)
{
	T temp = outVal1;
	outVal1 = outVal2;
	outVal2 = temp; 
}

template <class T>
void numDisplay(T array[], int size)
{
	for(int numbers; numbers < size; numbers++)
	{
		cout << array[numbers] << " ";
	}
	cout << endl;
}

// This function will sort out the numbers
template <class T>
void numSort(T array[], int size) 
{
	for(int counter = 0; counter < size - 1; counter++)
	{
		for(int track = counter + 1; track < size + 1; track++)
		{
			if (array[counter] > array[track])
			{
				numSwap(array[counter], array[track]);
			}
		}
	}
}


int main()
{
	// Declare class objects and storing array values
	SimpleVector<int> numTable;
	SimpleVector<float> floatTable;
	cout << "The values stored in the integer table: ";
	int numArray[SIZE] = {1, 12, 34, 56, 11, 15, 18, 20};
	numDisplay(numArray, 8);
	cout << "The numbers sorted in the integer table: ";
	numSort(numArray, 8);
	cout << endl; 

	cout << "The values stored in the floating table: ";
	float floatArray[SIZE] = {-9.0, 2.3, 1.3, 5.6, 7.8, 6.8, 12.0, 15.6};
	numDisplay(floatArray, 8);
	cout << "The numbers sorted in the floating table: ";
	numSort(floatArray, 8);
	system("PAUSE");
	return 0; 
}
Last edited on
Rather that taking on some functions to SimpleVector, I think you're expected to derive from SimpleVector to create SortedVector.
If I am understanding you correctly is that SimpleVector. h is the base class. The SortableVector class is the child class where its supposed to take in the data from the SimpleVector. h file. I thought I was doing that. I am not sure why that is it effecting the sorting alogrithm that I created.
Why numswap()? Just use std::swap()

http://www.cplusplus.com/reference/utility/swap/

I was asked to make a child class called SortableVector


But you haven't created a child class SortableVector. You've created variables numTable and floatTable but then don't use them...

There is also an issue in the sort routine. Using a sort function, then consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <utility>

constexpr size_t SIZE { 8 };

template <class T>
void numDisplay(const T array[], size_t size) {
	for (size_t numbers {}; numbers < size; ++numbers)
		std::cout << array[numbers] << " ";

	std::cout << '\n';
}

template <class T>
void numSort(T array[], size_t size) {
	for (size_t counter {}; counter < size - 1; ++counter)
		for (size_t track { counter + 1 }; track < size; ++track)
			if (array[counter] > array[track])
				std::swap(array[counter], array[track]);
}

int main() {
	int numArray[SIZE] { 1, 12, 34, 56, 11, 15, 18, 20 };

	std::cout << "The values stored in the integer table: ";
	numDisplay(numArray, 8);
	std::cout << "The numbers sorted in the integer table: ";
	numSort(numArray, 8);
	numDisplay(numArray, 8);

	double floatArray[SIZE] { -9.0, 2.3, 1.3, 5.6, 7.8, 6.8, 12.0, 15.6 };

	std::cout << "The values stored in the floating table: ";
	numDisplay(floatArray, 8);
	std::cout << "The numbers sorted in the floating table: ";
	numSort(floatArray, 8);
	numDisplay(floatArray, 8);
}



The values stored in the integer table: 1 12 34 56 11 15 18 20
The numbers sorted in the integer table: 1 11 12 15 18 20 34 56
The values stored in the floating table: -9 2.3 1.3 5.6 7.8 6.8 12 15.6
The numbers sorted in the floating table: -9 1.3 2.3 5.6 6.8 7.8 12 15.6

If I am understanding you correctly is that SimpleVector.h is the base class.
Correct.

The SortableVector class is the child class where its supposed to take in the data from the SimpleVector.h file.
I wouldn't word it like that. SimpleVector is the underlying container that holds the data. SortableVector has the interface that implements a sorted vector.

I thought I was doing that.
I don't believe you are. We'd expect to see a SortableVector class with methods that match SimpleVector's methods, but the content is always held in a sorted order.

You created separate functions that do stuff. It's not the same.
Topic archived. No new replies allowed.