I have this lab from a C++ class that I can't get to work...
Write a function containing a simple sort program that will sort an array of integers by using the stack class (as used in Ex_10). The array (pointer) and its length should be passed to the function as parameters so that the original will be sorted. The array created in main() should contain a maximum of 20 integers which should be entered at runtime (input to be ended by the value 0 or automatically when the 20th value is entered). Step through the array and place the largest number onto the stack (push). Each time you step through the array find the next smallest number and push it onto the stack. Assume no two numbers are duplicated in the array. When the stack is popped, all entries should appear in ascending order.
Here is the code I have so far...
#include <iostream>
using namespace std;
const int STACK_SIZE = 100;
class stack
{
private:
int count; // number of items in the stack
int data[STACK_SIZE];
public:
stack();
~stack();
void push(const int item); // push an item on the stack
int pop(void); // pop item off the stack
};
stack::stack()
{
count = 0; // zero the stack
}
stack::~stack() {}
void stack::push(const int item)
{
if (count < STACK_SIZE)
{
data[count] = item;
++count;
}
else cout << "Overflow!\n";
}
int stack::pop(void)
{
if (count >0)
{
--count;
return (data[count]);
}
else
{
cout << "Underflow!\n";
return 0;
}
}
void sort(int *x, int l)
{
int length = l, old = 0, big;
stack s;
for (int i = 0; i <l; i++)
{
big = x[0];
for (int j = 0; j <l; j++)
{
if (x[j] > big)
{
big = x[j];
old = j;
}
}
s.push(big);
x[old] = x[l-1];
l--;
}
for (int i = 0; i <l; i++)
x[i] = s.pop();
}
int main()
{
int a[21];
int count = 0;
do
{
cout << "Enter number "<<count+1<<", 0 to quit: ";
cin >> a[count];
count++;
}
while (a[count-1]!=0 && count <20);
sort(a, count);
for (int i = 0; i < count; i++)
cout << a[i] << endl;
return 0;
}