// L-12 MCS 572 Mon 6 Feb 2012 : use_stl_sort

// We illustrate how to sort an STL vector of doubles.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;

vector<double> random_vector ( int n );
// returns a vector of n random doubles

void write_vector ( vector<double> v );
// writes the vector v

struct less_than // defines "<"
{
   bool operator()(const double& a,
                   const double& b)
   {
      return (a < b);
   }
};

int main()
{
   cout << "give n : ";
   int n; cin >> n;

   srand(time(0));
   vector<double> v = random_vector(n);
   cout << n << " random doubles :\n";
   write_vector(v);

   sort(v.begin(),v.end(),less_than());

   cout << "the sorted vector : \n";
   write_vector(v);

   return 0;
}

vector<double> random_vector ( int n )
{
   vector<double> v;

   for(int i=0; i<n; i++)
   {
      double r = (double) rand();
      r = r/RAND_MAX;
      v.push_back(r);
   }
   return v;
}

void write_vector ( vector<double> v )
{
   for(int i=0; i<v.size(); i++)
      cout << scientific 
           << setprecision(15)
           << v[i] << endl;
}

