/* Chris von Krogh
   1.12 Problem Set 6 */

// macros and inclusions

#define null 0
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <math.h>
#include <fstream.h>
#include "Matrix.cc"

/******************************************************************/


double smirnoff (double arg){ if (arg<0) return -arg; return arg;}

int main() {
  Matrix A(8,10);  // first col is xi, second is abs(x-xi)
  double temp;
  ifstream data_str("data5.dat");
  for (int i=0;i<8;i++){
    data_str >> temp;
    A.set(i,0,temp);
    data_str >> temp;
    A.set(i,2,temp);}
  data_str.close();
  for (int i=0;i<8;i++){  // set the x-xis
    A.set(i,1,smirnoff(3-A.get(i,0)));}
  // now sort based on the x-xis
  for (int i=7;i>0;i--){
    for (int j=0;j<i;j++){
      if (A.get(j,1)>A.get(j+1,1))
	A.exchange(j,j+1);}}
  cout << endl << endl;
  for (int j=1;j<8;j++){
    for (int i=0;i<8-j;i++){
      temp=3-A.get(i,0);
      temp*=A.get(i+1,j+2-1);
      A.set(i,j+2,temp);
      temp=A.get(i+j,0);
      temp-=3;
      temp*=A.get(i,j+2-1);
      A.set(i,j+2,A.get(i,j+2)+temp);
      temp=A.get(i+j,0);
      temp-=A.get(i,0);
      A.set(i,j+2,A.get(i,j+2)/temp);
    }}
  A.print();
  cout << endl << "I would choose the next to last value, 2.90338." << endl;
}
