943,836 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 856
  • C++ RSS
Aug 1st, 2007
0

More sorting problems

Expand Post »
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...
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int STACK_SIZE = 100;
  5. class stack
  6. {
  7. private:
  8. int count; // number of items in the stack
  9. int data[STACK_SIZE];
  10. public:
  11. stack();
  12. ~stack();
  13. void push(const int item); // push an item on the stack
  14. int pop(void); // pop item off the stack
  15. };
  16. stack::stack()
  17. {
  18. count = 0; // zero the stack
  19. }
  20. stack::~stack() {}
  21. void stack::push(const int item)
  22. {
  23. if (count < STACK_SIZE)
  24. {
  25. data[count] = item;
  26. ++count;
  27. }
  28. else cout << "Overflow!\n";
  29. }
  30. int stack::pop(void)
  31. {
  32. if (count >0)
  33. {
  34. --count;
  35. return (data[count]);
  36. }
  37. else
  38. {
  39. cout << "Underflow!\n";
  40. return 0;
  41. }
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. void sort(int *x, int l)
  52. {
  53. int length = l, old = 0, big;
  54. stack s;
  55. for (int i = 0; i <l; i++)
  56. {
  57. big = x[0];
  58. for (int j = 0; j <l; j++)
  59. {
  60.  
  61. if (x[j] > big)
  62. {
  63. big = x[j];
  64. old = j;
  65. }
  66. }
  67. s.push(big);
  68. x[old] = x[l-1];
  69. l--;
  70. }
  71.  
  72. for (int i = 0; i <l; i++)
  73. x[i] = s.pop();
  74. }
  75.  
  76. int main()
  77. {
  78. int a[21];
  79. int count = 0;
  80. do
  81. {
  82. cout << "Enter number "<<count+1<<", 0 to quit: ";
  83. cin >> a[count];
  84. count++;
  85. }
  86. while (a[count-1]!=0 && count <20);
  87.  
  88.  
  89.  
  90. sort(a, count);
  91.  
  92. for (int i = 0; i < count; i++)
  93. cout << a[i] << endl;
  94.  
  95. return 0;
  96. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ayk-retail is offline Offline
48 posts
since Aug 2005
Aug 1st, 2007
0

Re: More sorting problems

Interestingly I didn't mention the problem in the main post. The problem is that it does not sort the code.
Reputation Points: 10
Solved Threads: 0
Light Poster
ayk-retail is offline Offline
48 posts
since Aug 2005
Aug 1st, 2007
0

Re: More sorting problems

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

look in the loop here
C++ Syntax (Toggle Plain Text)
  1. for (int i = 0; i <l; i++)
  2. {
  3. big = x[0];
  4. for (int j = 0; j <l; j++)
  5. {
  6.  
  7. if (x[j] > big)
  8. {
  9. big = x[j];
  10. old = j;
  11. }
  12. }
  13. s.push(big);
  14. x[old] = x[l-1];
  15. l--;
  16. }
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] )
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Aug 1st, 2007
0

Re: More sorting problems

I made some changes but it is still not working...
C++ Syntax (Toggle Plain Text)
  1. void sort(int *x, int l)
  2. {
  3. int length = l, old = 0, big;
  4. stack s;
  5. for (int i = 0; i <l; i++)
  6. {
  7. big = x[i];
  8. for (int j = i; j <l; j++)
  9. {
  10.  
  11. if (x[j] > big)
  12. {
  13. big = x[j];
  14. old = j;
  15. }
  16. }
  17. s.push(big);
  18. x[old] = x[l-1];
  19.  
  20. }
  21.  
  22. for (int i = 0; i <l; i++)
  23. x[i] = s.pop();
  24. }
Reputation Points: 10
Solved Threads: 0
Light Poster
ayk-retail is offline Offline
48 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Sorting int array with stack class
Next Thread in C++ Forum Timeline: help wanted asap





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC