Problem converting Selection Sort to Class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2006
Posts: 5
Reputation: Lomas is an unknown quantity at this point 
Solved Threads: 0
Lomas's Avatar
Lomas Lomas is offline Offline
Newbie Poster

Problem converting Selection Sort to Class

 
0
  #1
May 4th, 2006
hi everybody, this is the small c++ program for selection sort using functions.
  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.

  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....
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Problem converting Selection Sort to Class

 
0
  #2
May 4th, 2006
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:
  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
  1. ( sizeof(numbers) / sizeof(int) )
Or you can use a template trick to deduce the size of the array..
  1. template<int N>
  2. sort(int (&numbers)[N])
  3. { }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Problem converting Selection Sort to Class

 
0
  #3
May 4th, 2006
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
  1. int Selectionsort::size()
  2. {
  3. return ( sizeof(number)/sizeof(int) );
  4. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Problem converting Selection Sort to Class

 
0
  #4
May 4th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC