DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2007

    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...
    #include <iostream>
    using namespace std;
    const int STACK_SIZE = 100;
    class stack 
    		int count;			// number of items in the stack
    		int data[STACK_SIZE];
    		void push(const int item);	// push an item on the stack
    		int pop(void);			// pop item off the stack
    	count = 0;	// zero the stack
    stack::~stack() {}
    void stack::push(const int item)
    	if (count < STACK_SIZE)
    		data[count] = item;
    	else cout << "Overflow!\n";
    int stack::pop(void)
    	if (count >0)
    		return (data[count]);
    		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;
    		x[old] = x[l-1];
    	for (int i = 0; i <l; i++)
    		x[i] = s.pop();
    int main()
    	int  a[21];
    	int count = 0;
    		cout << "Enter number "<<count+1<<", 0 to quit: ";
    		cin >> a[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
    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, 04:32 AM
  2. Replies: 1
    Last Post: 04-05-2002, 10:18 AM
  3. Sorting for a JTable
    By Ramkumar in forum Java
    Replies: 1
    Last Post: 01-11-2001, 05:32 AM
  4. Replies: 0
    Last Post: 10-04-2000, 04:05 AM
  5. CrossBrowser DHTML table sorting problem
    By Joel Matto in forum Web
    Replies: 0
    Last Post: 05-02-2000, 03: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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.