/*************************************************
** This program reads in points from the user and
** prints them back out in sorted order from
** closest to the origin to farthest.
*************************************************/
#include <iostream>
#include <cmath>
using namespace std;
/*************************************************
** PROTOTYPES
*************************************************/
void selectionsort(double** A, int N);
/*************************************************
** MAIN
*************************************************/
int main()
{
// Get number of points and create & read array
int n;
cout << "How many points? ";
cin >> n;
cout << "Enter points (x,y,z): ";
double** A = new double*[n];
for(int i = 0; i < n; i++)
{
A[i] = new double[3];
char c;
cin >> c >> A[i][0]
>> c >> A[i][1]
>> c >> A[i][2] >> c;
}
// Sort points!
selectionsort(A,n);
// Print out points
for(int i = 0; i < n; i++)
{
cout << '(' << A[i][0]
<< ',' << A[i][1]
<< ',' << A[i][2] << ") ";
}
cout << endl;
return 0;
}
/*************************************************
** DEFINITIONS
*************************************************/
bool before(double* a, double* b)
{
return sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2])
<
sqrt(b[0]*b[0] + b[1]*b[1] + b[2]*b[2]);
}
void selectionsort(double** A, int N)
{
for (int i = 0; i < n - 1; ++i) {
// find nexti, the index of the next element
int nexti = i;
for (int j = i + 1; j < n; ++j) {
if (before(A[j], A[nexti])) {
nexti = j;
}
}
// swap A[i] and A[nexti]
double* temp = A[i];
A[i] = A[nexti];
A[nexti] = temp;
}
}