#include #include #include #include using namespace std; #if 1 int maxIndex(double* *array, int size); void selectionSort(double* *arrayIn, int sizeIn); void printPoint(double *point) { cout << "(" << point[0] << ", " << point[1] << ", " << point[2] << ")"; cout << endl; } double* readPoint(istream &fin) { double *point; point = new double[3]; fin >> point[0] >> point[1] >> point[2]; return point; } int main() { // create array and initialize with values int size ; cout << "How many values do you want to enter? "; cin >> size; double* * array = new double*[size]; // open file ifstream fin("data.txt"); cout << "Enter a list of " << size << " values: " <> array[i][0] >> array[i][1] >> array[i][2]; } // sort the array selectionSort(array, size); // display the array for (int j = 0; j < size; j++) { printPoint(array[j]); } cout << endl; return 0; } bool isBefore (double* t1, double* t2) { // basic ordering: x^2 + y^2 + z^2 double mag1 = (t1[0]*t1[0] + t1[1]*t1[1] + t1[2]*t1[2]); double mag2 = (t2[0]*t2[0] + t2[1]*t2[1] + t2[2]*t2[2]); return mag1 < mag2; } int maxIndex(double* *array, int size) { int maxIndex = 0; for (int i=1; i < size; i++) { if (isBefore(array[maxIndex],array[i]) ) { maxIndex = i; } } return maxIndex; } void swap (double* &thing1, double* &thing2) { double* temp = thing1; thing1 = thing2; thing2 = temp; } void selectionSort(double* *arrayIn, int sizeIn) { for(int size = sizeIn; size > 1; size--) { int k = maxIndex(arrayIn,size); swap(arrayIn[k], arrayIn[size-1]); } } #endif