Well your on the right track with
void Sort(int *x, int size);
. I would suggest you have a look at this:
http://www.cplusplus.com/doc/tutorial/pointers/ to get a good understanding of pointers.
If you look at your existing Bubble Sort it uses array notation to cycle through the array; in your newly created function you would need to use pointer notation to access the different addresses where your 5 items are stored.
So, with that in mind, if I were accessing the 2nd element of my array using normal array notation I would use
array[1]
, if I were to do that with pointers I would need to use
*(array+1)
.
This is one of those posts where you think.. would it be beneficial to the OP if I wrote the solution to the problem, on this occasion I feel it would be beneficial... pointers are a pain in the butt, but once you master them your well on the way to becoming a good programmer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
void Sort(int*, int);
int main()
{
int x[] = { 28, 87, -3, 45, 19 };
Sort(x, 5);
return 0;
}
void Sort(int *data, int size)
{
int temp;
for (int i = 0; i < size; i++)
for (int j = 0; j < size - i - 1; j++)
if (*(data+j) > *((data+j)+1))
{
temp = *(data + j);
*(data + j) = *((data + j) + 1);
*((data + j) + 1) = temp;
}
}
|
If you have any questions please feel free to ask, pointers do take time to get your head around. :)