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