| | |
More sorting problems
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2005
Posts: 48
Reputation:
Solved Threads: 0
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...
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...
C++ Syntax (Toggle Plain Text)
#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; }
The problem would appear to be that you're not sorting your entire list, only half of it.
look in the loop here 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] )
look in the loop here
C++ Syntax (Toggle Plain Text)
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--; }
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] )
¿umop apisdn upside down? •
•
Join Date: Aug 2005
Posts: 48
Reputation:
Solved Threads: 0
I made some changes but it is still not working...
C++ Syntax (Toggle Plain Text)
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(); }
![]() |
Similar Threads
- Why put up with the pain. Change browsers! (Web Browsers)
- blue imac 0s install not working (OS 7 / 8 / 9)
- internet filter (Windows Software)
- Sorting Algorithms using Time (C++)
- Sorting arrays of pointers with function? (C)
Other Threads in the C++ Forum
- Previous Thread: Sorting int array with stack class
- Next Thread: help wanted asap
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





