//Program 1.2.c
#include <stdlib.h>
#include <math.h>
#include <fstream.h>
#include <iostream.h>
#include <strstream.h>
#include <time.h>

int main(){
  //A simple minded program for a simple problem:  We are asked
  //in problem 1.1 to create 10,000 random numbers, and to bin them
  //in bins of length 0.01 (0,1) and to plot the histogram.  There 
  //will be 100 bins total

  //actually...not random in this program, but the logistic map

  ofstream outf("1.1.rand.dat");
  srand(time(NULL));
  rand();
  int NUM=10000;
  int H[99]; double R[NUM-1];  
  //We use the map x' = \lambda x (1-x)
  R[0]=0.3; float lamb=3.8;
  for (int i = 1; i<NUM; i++){
    R[i]=lamb*R[i-1]*(1-R[i-1]);
  }//printf("%f \t",R[i]);}

  for (int i=0; i<100;i++){H[i]=0;}
  for (int i =1; i<NUM; i++){
    R[i-1]=sqrt((R[i]-R[i-1])*(R[i]-R[i-1]));}
  //Assignment is such as 0.01 > 1, 0.02 > 2 onto 100 
  for (int j = 0; j<100;j++){
  for (int i = 0; i<NUM; i++){
    if (R[i]<=(0.01 * (j+1))) if (R[i]>=(0.01*(j))) {H[j] = H[j]+1;}}}
    
  for (int i = 0; i<100; i++){
    outf<<H[i]<<"\n";}

  //in this case, n = 10000, p_S = 100 for each bin.
  double V = 0.0;
  double v = 0.0;
  float COUNT = 0.0;
  for (int i = 0; i<100; i++){if(H[i] > 0){COUNT=COUNT+1;}}
  float COUNTER;
  COUNTER = NUM/COUNT;
  for (int i = 0; i<100; i++){if(H[i] > 0){v = v+ (H[i]*H[i])/COUNTER;}}
  V = v - 1.0*NUM;
  printf("%f \n",V);
  printf("%f \n",COUNT);
}
