//*****************************************************************************
////  Course:             COMS 2203 Foundations of Computer Programming II
////  Semester:           Fall 2004
////  Assignment Number:  #2                             
////  Author Name:        DarC KonQuesT                        
////  Date Written:       August 27, 2004
////                                                         
////  Description of Program:                              
////      
////                               
////*****************************************************************************
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

const int N = 100; // array size (max number of items that can be stored)

void sort(float sale[], int a[], int n)
{

 int smallestIndex, index, minIndex, temp2;
 float temp1;
 for(index = 0; index < n - 1; index++)
 {
   smallestIndex = index;
   for(minIndex = index + 1; minIndex < n; minIndex++)
     if(sale[minIndex] > sale[smallestIndex])
       smallestIndex = minIndex;

   temp1 = sale[smallestIndex];
   temp2 = a[smallestIndex];
   
   sale[smallestIndex] = sale[index];
   a[smallestIndex] = a[index];
   
   sale[index] = temp1;
   a[index] = temp2;
 }

}

int main()
{
  ifstream infile;
  ofstream outfile;
  string fname;
  int x;     // ID# from file
  float id;  // Sales amount from file
  int k;     // number of items stored in array, array subscript (index)
  int a[N];  // ID# Array
  float sale[N]; 
  string ofilename;
 
  cout << "Enter input file name:  ";  
  cin >> fname;
  cout << "Enter output file name: ";
  cin >> ofilename;
  infile.open(fname.c_str());
  if(!infile) {
    cerr << "Error: Can't open file." << endl;
    return 1;
  } 
  
  k = 0;         // initialize array subscript value
  infile >> x;   // read 1st item
  infile >> id;
  while(infile && k < N) { // if not eof and array has space for item
    a[k] = x;    // store item in array              
    sale[k] = id;
    ++k;         // next location in array, number of items in array
    infile >> x; // read next item              
    infile >> id;
  }

  sort(sale, a, k);
  outfile << fixed << showpoint << setprecision(2); 
  outfile.open(ofilename.c_str());
  
  for(int i = 0; i < k; ++i)
    outfile << a[i] << "    " << sale[i] << endl; 
  return 0;
}

