944,035 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11059
  • C++ RSS
Feb 11th, 2007
-1

bubble sort function w/ array

Expand Post »
The assignment is to write a C++ function using "pointer notation" that will write out the elements of an array of int in reverse order.

The problem I'm having is with the sortAscend function and what the parameters should be to accept an array. I'm getting an error cause I'm passing an object and the function expects an int pointer.

C++ Syntax (Toggle Plain Text)
  1. class arrayList
  2. {
  3. public:
  4. void print() const;
  5. //Function to output the elements of the list
  6. //Postcondition: Elements of the list are output
  7. // on the standard output device.
  8.  
  9. void sortAscend(int *list, int length);
  10. //Function to sort the array in Ascending order
  11. //Postcondition: Elements of the list are sorted
  12. // in Ascending order
  13.  
  14. void insertEnd(int insert);
  15. //Function to insert elements at the end of list
  16. //Postcondition: list[length] = insert; length++;
  17.  
  18. arrayList(int size = 10);
  19. //constructor
  20. //Creates an array of the size specified by the
  21. //parameter size. The default size is 10.
  22. //Postcondition: The list points to the array,
  23. // length = 0, and maxSize = size;
  24.  
  25. ~arrayList();
  26. //destructor
  27. //Deallocate the memory occupied by the array.
  28.  
  29. private:
  30. int *list; //array to hold the list elements
  31. int length; //variable to store the length of the list
  32. int maxSize; //variable to store the maximum size of the list
  33. };
C++ Syntax (Toggle Plain Text)
  1. #include "arrayList.h"
  2.  
  3. using namespace std;
  4.  
  5. void arrayList::print() const
  6. {
  7. for (int i = 0; i < length; i++)
  8. cout << list[i] << " ";
  9. cout << endl;
  10. } //end print function
  11.  
  12. void arrayList::sortAscend(int *list, int length)
  13. {
  14. for(int i = 0; i < length; i++)
  15. {
  16. for(int j = 0; j < length - i; j++)
  17. {
  18. if(list[i] > list[j + 1])
  19. {
  20. int temp = list[j]; //swap
  21. list[j] = list[j + 1];
  22. list[j + 1] = temp;
  23. }
  24.  
  25. }
  26.  
  27. }
  28.  
  29. } //end sortAscend function
  30.  
  31. void arrayList::insertEnd(int insert)
  32. {
  33. if (length >= maxSize) //the list is full
  34. cout << "Cannot insert in a full list." << endl;
  35.  
  36. else
  37. {
  38. list[length] = insert; // insert the item at the end of the list
  39. length++; //increment the length
  40. }
  41. } //end function insert
  42.  
  43. arrayList::arrayList(int size)
  44. {
  45. if (size <= 0)
  46. { cout << "The array size must be positive. Creating "
  47. << "an array of the size 10." << endl;
  48.  
  49. maxSize = 10;
  50. }
  51. else
  52. maxSize = size;
  53.  
  54. length = 0;
  55. list = new int[maxSize];
  56. } //end constructor
  57.  
  58. arrayList::~arrayList()
  59. {
  60. delete [] list;
  61. } //end destructor
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. #include "arrayList.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. arrayList list(10);
  10.  
  11. int number;
  12.  
  13. cout << "Enter 10 integers: ";
  14.  
  15. for (int count = 0; count < 10; count++)
  16. {
  17. cin >> number;
  18. list.insertEnd(number);
  19. }
  20.  
  21. cout << "Before sorting, array[10]: ";
  22.  
  23. list.print();
  24.  
  25. cout << endl;
  26.  
  27. list.sortAscend(*list, 10);
  28.  
  29. }
Similar Threads
Reputation Points: 41
Solved Threads: 0
Junior Poster in Training
SHWOO is offline Offline
73 posts
since Jul 2006
Feb 11th, 2007
0

Re: bubble sort function w/ array

Since you are calling the function of the object (ie the member function) you don't need to pass anything to the function, since the object contains all that is needed to sort its elements. Just don't forget to update the length of the internal list and it should be fine.

Also write another function which checks whether the Object contains atleast one element before sorting so as to avoid crashes.

Something like:
  1. void arrayList::sortAscend ()
  2. {
  3. // here length and list are the member variables of the object
  4. // & and hence can be directly accessed
  5. for(int i = 0; i < length; i++)
  6. {
  7. for(int j = 0; j < length - i; j++)
  8. {
  9. if(list[i] > list[j + 1])
  10. {
  11. int temp = list[j]; //swap
  12. list[j] = list[j + 1];
  13. list[j + 1] = temp;
  14. }
  15. }
  16. }
  17. } //end sortAscend function

Oh, and btw, your sorting logic is flawed. A simple fix would be:
  1. for(int i = 0; i < length - 1; i++)
  2. {
  3. for(int j = i + 1; j < length; j++)
  4. {
  5. if(list[i] > list[j])
  6. {
  7. int temp = list[i]; //swap
  8. list[i] = list[j];
  9. list[j] = temp;
  10. }
  11. }
  12. }
Last edited by ~s.o.s~; Feb 11th, 2007 at 2:56 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Feb 11th, 2007
0

Re: bubble sort function w/ array

Well from what I can dig out of my very shallow pool of knowledge it seems that in your prototype you are telling it that you want it to accept a pointer but when you call the function you are passing by value instead of by reference IE. &list
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Feb 11th, 2007
0

Re: bubble sort function w/ array

Thanks for the help. I just deleted the function with the two parameters. I got the code for that bubble sort algorithm straight out of the book so I am surprised it was flawed, but you were right cause when I compiled I get an error then I ran it your way and it worked great. Thanks again.
Reputation Points: 41
Solved Threads: 0
Junior Poster in Training
SHWOO is offline Offline
73 posts
since Jul 2006
Feb 11th, 2007
0

Re: bubble sort function w/ array

It would really be interesting to know which book you are referring.

A book with flawed algorithm of such a simple sort is not a book worth reading....
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Feb 11th, 2007
0

Re: bubble sort function w/ array

C++ Programming: Program Design Including Data Structures 3rd ed. by D.S. Malik

I am going to copy the algorithm again maybe I screwed it up.

C++ Syntax (Toggle Plain Text)
  1. void bubbleSort(elemType list[], int length)
  2. {
  3. for (int i = 1; i < length; i++)
  4. {
  5. for (int j = 0; j < length - i; j++)
  6. {
  7. if (list[j] > list[j + 1])
  8. {
  9. elemType temp = list[j];
  10. list[j] = list[j + 1];
  11. list[j + 1] = temp;
  12. }
  13. }
  14. }
  15. }
Reputation Points: 41
Solved Threads: 0
Junior Poster in Training
SHWOO is offline Offline
73 posts
since Jul 2006
Feb 11th, 2007
0

Re: bubble sort function w/ array

So I went back and looked at my first algorithm and I had in the if statement (list[i] >
and it should have been (list[j] >

I like your algorithm because it is a little simpler and more clear. Thanks again for the help.

Reply to ya later.
Reputation Points: 41
Solved Threads: 0
Junior Poster in Training
SHWOO is offline Offline
73 posts
since Jul 2006

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: hw help needed - dont want code
Next Thread in C++ Forum Timeline: file problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC