sorting arrays (ascending order)
im trying to sort an array with an Array class. here's my code...i picked up some code from a website and stuck it into my sort method...i don't know how else to do it though...note that the sort method has no parameters...that was strictly instructed by my teacher.
Code:
class Array
{
private:
const static int declared_size = 100;
arrElem array[declared_size];
int numberUsed;
public:
void readFromFile(char fileName[])
{
ifstream infile(fileName);
int filesize = 0;
if (infile.fail())
{
cout << "Opening file " << fileName << " failed. Goodbye." << endl;
exit(1);
}
infile >> ws;
while (infile >> array[filesize])
{
filesize++;
if (filesize > 100)
{
cout << "There are more values in the file than there are positions"
<< " in the array."
<< endl;
exit(1);
}
}
numberUsed = filesize;
infile.close();
}
int getSize(void)
{
return numberUsed;
}
void printArray(void)
{
int perLine = 5;
for (int i = 0; i < numberUsed; i++)
{
cout << array[i] << " ";
if ((i+1) % perLine==0)
cout << endl;
}
}
void sort(void)
{
for (int i = 0; i < numberUsed; i++)
{
double x = array[i];
int j;
for ( j = 0; j < numberUsed; j++)
{
if (array[j] <= x)
break;
array[j+1] = array[j];
}
array[j+1] = x;
}
}
};