I figured out the reason behind the scenes of my last post; however, I have a problem that I want to get solved. My code that is useful to this post is below:
class numbers
{
public:
void add(int n); // Adds a new number to the end of the vector
int remove(int i); // Removes the element at the specified index of the vector
void sort_up(); // Calls the private function sort_up function
void sort_down(); // Calls the private recursive sort_down function
void display(); // Prints the vector to the screen
private:
vector <int> numbers;
vector <int> sort_down(vector <int> unsorted_list);
vector <int> sort_up(vector <int> unsorted_list);
};
int numbers::remove(int index)
{
if ((index >= 0) && (index + 1 <= numbers.size()))
{
numbers.erase(numbers.begin() + index);
for (int i = 0; i < numbers.size(); i++)
cout << numbers[i] << " ";
cout << endl;
}
else
{
cout << "Invalid index, element either below zero or has exceeded the size of the array";
cin.get();
}
}
void numbers::sort_down()
{
numbers = sort_down(numbers);
}
vector <int> numbers::sort_down(vector <int> unsorted_list)
{
int max, count = 0;
if (unsorted_list.size() == 1)
{
cout << unsorted_list[0];
}
else
{
max = unsorted_list[0];
for (int i = 0; i < unsorted_list.size(); i++)
{
if (max < unsorted_list[i])
{
max = unsorted_list[i];
count = i;
}
}
cout << max << " ";
numbers::remove(count);
numbers::sort_down();
}
}
My first problem is that my sort function isn't really in the way that I envisioned. My purpose of the sort_down function is to find the maximum value in the array, remove that element and then continue until the size of the array is one. My problem is that I am removing/outputting the max. element but I cannot seem to get around displaying the entire array every iteration. A simple example of what I my screen looks like: