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;
}

Recommended Answers

All 3 Replies

Interestingly I didn't mention the problem in the main post. The problem is that it does not sort the code.

The problem would appear to be that you're not sorting your entire list, only half of it.

look in the loop here

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--;
	}

At the same time as incrementing i, you're decreasing 'l' - which means the for loop stops when you're halfway through the list (the two values converge in the middle).

there are all sorts of ways of doing sorts, but i suggest you leave your 'l' variable alone - also, your inner loop probably wants to start at j = i rather than j=0 (Similarly, your 'big' number probably wants to start at x )

I made some changes but it is still not working...

void sort(int *x, int l)
{
	int length = l, old = 0, big;
	stack s;
	for (int i = 0; i <l; i++)
	{
		big = x[i];
		for (int j = i; j <l; j++)
		{	
			
			if (x[j] > big)
			{
				big = x[j];
				old = j;
			}
		}
		s.push(big);
		x[old] = x[l-1];
		
	}

	for (int i = 0; i <l; i++)
		x[i] = s.pop();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.