hi everybody, this is the small c++ program for selection sort using functions.

#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.

#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....

Recommended Answers

All 3 Replies

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::Selectionsort(int* begin, int* end) or one which accepts an array, eg, Selectionsort::Selectionsort(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:

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

( sizeof(numbers) / sizeof(int) )

Or you can use a template trick to deduce the size of the array..

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

int Selectionsort::size()
{
   return ( sizeof(number)/sizeof(int) );
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.