#include <stdlib.h>
#include <math.h>
#include <fstream.h>
#include <iostream.h>
#include <strstream.h>
#include <time.h>

//------>START PROGRAM HERE
int main(){
  //THIS PROGRAM IS FOR PROBLEM 1 of HOMEWORK 2 NLD
  
  //WE CONSTRUCT TWO SETS OF RANDOM NUMBERS
  //ONE IS THE BASE SET
  
  //THE OTHER IS THE EMPIRICAL SET

  //WE THEN DO A NEAREST NEIGHBOR FORWARD ITERATION 
  //COMPARISON;  THEN WE HISTOGRAM DIFFERENCES

  //------>PREP THE COMPUTER FOR PRINTING TO FILE
  ofstream outf("pro1.dat");
  //The output file will be pro1.dat
  ifstream inf("randomlist.dat");

  //***SHADOW PROGRAMMING,
  //***ignore the stuff with astericks
  ofstream outf2("test1.dat"); //************
  ofstream outf3("test2.dat"); //************
  ofstream outf4("test3.dat"); //************
  //*****************************************


  //------>DECLARE VARIABLES HERE
  int NUM=10000;//as requested
  double diff,r;
  double STORAGE[NUM];
  double WORK[NUM];
  double DIFF[NUM];
  double H[200];
  int MARK[NUM];
  int flag=0;
  double difff,different;
  int marker;
  //These will be self explanatory

  //------>PREP the RNG
  srand(time(NULL));
  rand();
  
  int k=0;
  int ii=0;
  char buffer[1900];
  //------>ACTION
  //The database and empirical
  while (inf){
    inf.getline (buffer,1900);
    istrstream iss(buffer,strlen(buffer));
    iss >> STORAGE[ii]; ii= ii+1;}
  //for(int i=0; i<NUM; i++){STORAGE[i] = 1.0*rand()/(1.0*RAND_MAX);}

  for(int i=0; i<NUM*5; i++){
    r=1.0*rand()/(1.0*RAND_MAX);
    flag=flag+1; 
    if(flag>=8){WORK[k]=r;k++;}if(flag==9){flag=-1;}
    //***WORKS HERE printf("%f \n",WORK[i/8]);}
  } 
  //****printf("%i \n",k);  Both random sequences are flush
  //The database and empirical group now being compsed,
  //we find the nearest neighbor and get the difference:

  for(int i=0; i<NUM-1; i++){
  difff=100;
    for(int j=0;j<NUM;j++){
      different=fabs(WORK[i]-STORAGE[j]);
      if(different<difff){difff=different; MARK[i]=j;}
    }}
  //***printf("%i \n",marker);}
  //MARK gives us a list of the nearest match that corresponds
  //with the ith entry of WORK
  
  //Now we can project and find the next iteration in each, and 
  //get the difference thereof
  int m;
  for(int i=0; i<NUM-1;i++){
    m = MARK[i];
    //***printf("%i \n",m);
    DIFF[i]=WORK[i+1]-STORAGE[m+1] +1;
   }
  //Finally, we want to bin these numbers.  
  //The differences should range from 0 to +/- 1
  //We'll do a binning of 0.01
  //This is 200 bins total
  //We multiply the differences by 100
  //and round to the lowest integer (1.9 --> 190; 1.96-->1.9)
  //int jj;
  //for(jj=0; jj<NUM-1; jj++){    
  //for(int i=0; i<200; i++){
  //  if(DIFF[jj]==i){H[i]=H[i]+1;}
  //}}
  //
  for(int i=0; i<200;i++){H[i]=0;}
 
  for (int j = 0; j<200;j++){
  for (int i = 0; i<NUM; i++){
    if (DIFF[i]<=(0.01 * (j+1))) if (DIFF[i]>=(0.01*(j))) {H[j] = H[j]+1;}}}
    
 
  for(int i=0; i<200;i++){
  outf<<(1.0*i)/100.0 -1.0<<"\t"<<H[i]<<"\n";
   }

  for(int j=0; j<NUM;j++){
    outf2<<STORAGE[j]<<"\n";
    outf3<<WORK[j]<<"\n";
    outf4<<DIFF[j]<<"\n";
  }

  //CHI^2 TEST
  double V = 0.0;
  double v = 0.0;
  for (int i = 1; i<100; i++){v = v+ ((H[i]-i)*(H[i]-i))/i;}
  for (int i = 101; i<200; i++){v = v+ ((H[i]-200+i)*(H[i]-200+i))/i;}
  V = v;
  printf("YOUR CHI^2 RESULT IS THE FOLLOWING NUMBER, THIS BEING EVIDENCE THAT YOU DID NOT JUST MAKE IT UP OUT OF THIN AIR: %f ",V);


}  
  
