Sorting problems


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Sorting problems

  1. #1
    Join Date
    Aug 2007
    Posts
    1

    Exclamation Sorting problems

    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...
    Code:
    #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;
    }

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    what does it do, push the largest value each time?

Similar Threads

  1. Problems with MSSQLServer and Java
    By jlopes151 in forum Database
    Replies: 1
    Last Post: 11-21-2005, 05:32 AM
  2. Replies: 1
    Last Post: 04-05-2002, 11:18 AM
  3. Sorting for a JTable
    By Ramkumar in forum Java
    Replies: 1
    Last Post: 01-11-2001, 06:32 AM
  4. Replies: 0
    Last Post: 10-04-2000, 05:05 AM
  5. CrossBrowser DHTML table sorting problem
    By Joel Matto in forum Web
    Replies: 0
    Last Post: 05-02-2000, 04:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center