Hello all,
This is some code that another poster put up a while ago.
Just for the heck of it, I tried to compile his solution, but i can't get the poiner syntax on the bubblesort function to work. I modified the call in main() to take a ref instead of *array[], in order to get it to actually even ATTEMPT to enter the function. Otherwise I got a plethora of compiler complaints. I still, do but now they are confined to the function definition in this present form. I get a segmentation violation when strcmp() tries to read the arrays in.
You might ask why I don't use ref syntax instead? Thats because the OP had it this way, and I would like to better understand how to structure code that uses pointers.
Thanks in advance!
#include<iostream>
using namespace std;
#include<cstring>
using std::strncmp;
void BubbleSort( const char *array[] , int size );
int main()
{
const int ArraySize = 10;
const char * Array[] = { "Toronto", "Montreal", "Alberta", "Quebec", "NewYork", "Calgary", "Edmonton", "NewJersey", "Ontario", "California" };
cout << "The unsorted arangement of the town names is: \n"<<endl;
for ( int i = 0; i < ArraySize; i++ )
{
cout <<" " << Array[i];
cout << endl << "\n";
}
BubbleSort( &Array[ArraySize] , ArraySize);
cout << "The sorted arangement of the town names is: \n"<<endl;
for ( int j = 0; j < ArraySize; j++ )
{
cout <<" " << Array[j];
cout << endl << "\n";
}
return 0;
}
void BubbleSort( const char *array[] , int size )
{
int result;
for ( int next = 0; next < size - 1 ; ++next )
{
for ( int j = 0; j < size - 1 - next; ++j )
{
result = (strcmp (( array[j]),( array[j+1])));
if (result > 0)
swap ( array[j] , array[j+1] );
}
}
}