944,082 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3155
  • C++ RSS
May 4th, 2006
0

Problem converting Selection Sort to Class

Expand Post »
hi everybody, this is the small c++ program for selection sort using functions.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. void selectionSort(int numbers[], int array_size);
  7.  
  8. int main () {
  9. int number[4]= {20, 10, 40, 30};
  10. selectionSort(number,4);
  11. for (int i=0;i<4;i++){
  12. cout<<number[i]<<endl;
  13. }
  14. system("Pause");
  15. return 0;
  16. }
  17.  
  18.  
  19.  
  20. void selectionSort(int numbers[], int array_size)
  21. {
  22. int i, j;
  23. int min, temp;
  24.  
  25. for (i = 0; i < array_size-1; i++)
  26. {
  27. min = i;
  28. for (j = i+1; j < array_size; j++)
  29. {
  30. if (numbers[j] < numbers[min])
  31. min = j;
  32. }
  33. temp = numbers[i];
  34. numbers[i] = numbers[min];
  35. numbers[min] = temp;
  36. }
  37. }

and i would like to change the same selection sort program using class, and following is the source code for selection sort with class.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Selectionsort {
  6. private:
  7. int i, j;
  8. int numbers[];
  9. int array_size;
  10. int min, temp;
  11. public:
  12.  
  13. void sort(int[], int);
  14. void print();
  15. };
  16.  
  17.  
  18. void Selectionsort::sort(int numbers[], int array_size)
  19. {
  20. for (i = 0; i < array_size-1; i++)
  21. {
  22. min = i;
  23. for (j = i+1; j < array_size; j++)
  24. {
  25. if (numbers[j] < numbers[min])
  26. min = j;
  27. }
  28. temp = numbers[i];
  29. numbers[i] = numbers[min];
  30. numbers[min] = temp;
  31. }
  32. } ;
  33.  
  34. void Selectionsort::print() // to print out the sorted numbers
  35. {
  36. for (int i=0;i<4;i++){
  37. cout<<numbers[i]<<endl;
  38. }
  39. };
  40.  
  41.  
  42. int main () {
  43. Selectionsort a[4] = {20,10,40,30}; //create an object
  44. a[4].sort(a,4); //sort
  45. a[4].print(); //print out the sorted numbers
  46. system("Pause");
  47. return 0;
  48. }

unfortunately i got compilation errors, i have no idea how to correct them:rolleyes:
can someone help me to find out is there any syntax errors for the second source code?
i really appreciate your help....
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Lomas is offline Offline
5 posts
since Apr 2006
May 4th, 2006
0

Re: Problem converting Selection Sort to Class

Your problem is that you don't have a constructor for the Selectionsort class which accepts the values you're trying to initialise it with. You should try a constructor which accepts a range of values - eg,
Selectionsort:electionsort(int* begin, int* end)
or one which accepts an array, eg,
Selectionsort:electionsort(int arr[])

Whichever of these you use, I don't think you can use the initialisation syntax as it stands in main() - you would probably need to declare the array seperately first before passing it to the Selectionsort constructor:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. const int foo_size(5);
  4. int foo[foo_size] = {1,3,5,7,9};
  5. //Initialisation of Selectionsort object: either
  6. Selectionsort bar(foo, foo+foo_size);
  7. // Or...
  8. Selectionsort bar(foo);
  9. }

A couple of other comments - you don't need to pass the array size to the function, you can work it out using
C++ Syntax (Toggle Plain Text)
  1. ( sizeof(numbers) / sizeof(int) )
Or you can use a template trick to deduce the size of the array..
C++ Syntax (Toggle Plain Text)
  1. template<int N>
  2. sort(int (&numbers)[N])
  3. { }
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
May 4th, 2006
0

Re: Problem converting Selection Sort to Class

Actually.. you don't need to pass the array or the size at all - because both data members are stored in your class. You probably want a blank parameter list for the sort function... And since you can work out the size of the array, you shouldn't need to store it as a data member either, you could create a member function to work it out
C++ Syntax (Toggle Plain Text)
  1. int Selectionsort::size()
  2. {
  3. return ( sizeof(number)/sizeof(int) );
  4. }
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
May 4th, 2006
0

Re: Problem converting Selection Sort to Class

You may be experiencing ambiguity. int numbers[] and array_size passed to sort() as parameters are the same names as member variables. How is the compiler to know which to use?

Also, the member variable int numbers[] is never allocated any memory, yet you try to use it in print().

Try commenting out the member variables numbers[] and array_size and the print() function and recompile.

I have to wonder why you want to put this in a class at all, but if that's your goal, then you have two choices. You can either use an array member variable and copy data from the array you want to sort into the member variable array and sort the member variable array, or you can remove the member array variable, pass an array, and use it as is, passing it from method to method as needed. If you decide to use a member array variable then you need to allocate memory for it. You can either use static memory to hard code the size at compilation time or you can use dynamic memory to allow run time sizing.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 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: doubly linked list--- help needed1
Next Thread in C++ Forum Timeline: temperature help





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


Follow us on Twitter


© 2011 DaniWeb® LLC