| | |
Problem converting Selection Sort to Class
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
hi everybody, this is the small c++ program for selection sort using functions.
and i would like to change the same selection sort program using class, and following is the source code for selection sort with class.
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....
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void selectionSort(int numbers[], int array_size); int main () { int number[4]= {20, 10, 40, 30}; selectionSort(number,4); for (int i=0;i<4;i++){ cout<<number[i]<<endl; } system("Pause"); return 0; } void selectionSort(int numbers[], int array_size) { int i, j; int min, temp; for (i = 0; i < array_size-1; i++) { min = i; for (j = i+1; j < array_size; j++) { if (numbers[j] < numbers[min]) min = j; } temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } }
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)
#include <iostream> using namespace std; class Selectionsort { private: int i, j; int numbers[]; int array_size; int min, temp; public: void sort(int[], int); void print(); }; void Selectionsort::sort(int numbers[], int array_size) { for (i = 0; i < array_size-1; i++) { min = i; for (j = i+1; j < array_size; j++) { if (numbers[j] < numbers[min]) min = j; } temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } } ; void Selectionsort::print() // to print out the sorted numbers { for (int i=0;i<4;i++){ cout<<numbers[i]<<endl; } }; int main () { Selectionsort a[4] = {20,10,40,30}; //create an object a[4].sort(a,4); //sort a[4].print(); //print out the sorted numbers system("Pause"); return 0; }
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....
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,
or one which accepts an array, eg,
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:
A couple of other comments - you don't need to pass the array size to the function, you can work it out using Or you can use a template trick to deduce the size of the array..
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)
int main() { const int foo_size(5); int foo[foo_size] = {1,3,5,7,9}; //Initialisation of Selectionsort object: either Selectionsort bar(foo, foo+foo_size); // Or... Selectionsort bar(foo); }
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)
( sizeof(numbers) / sizeof(int) )
C++ Syntax (Toggle Plain Text)
template<int N> sort(int (&numbers)[N]) { }
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)
int Selectionsort::size() { return ( sizeof(number)/sizeof(int) ); }
•
•
Join Date: Jul 2005
Posts: 1,673
Reputation:
Solved Threads: 261
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.
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.
![]() |
Similar Threads
- Selection Sort in java (Java)
- Selection Sort and String (C++)
- Some help needed with Selection Sort in java, Please!! (Java)
Other Threads in the C++ Forum
- Previous Thread: doubly linked list--- help needed1
- Next Thread: temperature help
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui 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 return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






