@
Golden Lizard
I've seen this style a few times recently. It's impressively geeky, but doesn't make much sense in the context of a templated function. For example if main looked like this, then it would fail to compile
1 2 3 4
|
std::string y[]={ "1", "5", "9", "2", "8" ,"0" };
functor<std::string,sizeof(y)/sizeof(y[0])> bubble;
bubble(y);
bubble.print(y);
|
Though there is of course std::swap, when writing code as an example for a beginner, it's better to stick with something more simple:
1 2 3 4 5 6
|
void swap(data & a, data & b)
{
data temp = a;
a = b;
b = temp;
};
|
@
kais2 Apologies for the distraction.
I took a look at your code, and it seems that there are a number of errors.
I would start with something simple, then build up gradually, one step at a time. Here's a start, with a constructor which takes an array of integers and its size.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
class sort {
private:
int size; // number of items in the array
int *arr; // pointer to the array of integers
public:
sort(int array[], int length);
};
sort::sort(int array[], int length)
{
arr = array;
size = length;
}
int main()
{
int a[10] = {9,3,5,6,1,7,2,0,8,4};
sort bubble(a, 10);
}
|
Next, I suggest adding the
print
function - that will be useful throughout all the remaining steps.
The
swap
function will be pretty simple, just three lines.
The
sort
function will involve the loops somewhat similar to those you already have.