/* Central Limit Theorem Demonstration: For lab tried to remove all the GSL calls and replace them with standard mathIO (inferior) rand(). *
/* re-edit for C++ and to print to file, formatting, and true random by David Bernard 10-31-2016 *
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <ctime>

using namespace std;

int main (int argc, char *argv[])
{
    srand(time(0));
    double u, uprev, avgout;
    int i, n, j, flag, avgn, nout, seed;
    seed = (rand());
    cout << "Enter N (Number of trials): " << endl;
    cin >> nout;
    cout<< "Enter number of times to average the data: " << endl;
    cin >> avgn;
    for (i = 0; i <seed+1; i++)
        {
            double u = (rand()/1.0);
        }
    std::ofstream ofs ("CLT.txt", std::ofstream::out);
    for (j = 0; j<nout; j++)
        {
            uprev = 0.0;
            flag = 1;
            n = avgn;
            avgout = 0;
            for (i = 0; i <n; i++)
                {
                    flag =1;
                    double u = ((double) rand())/((double) RAND_MAX);
                    if ( (u<.35)&&(u>.25))
                        {
                            n=n+1;
                            flag = 0;
                        }
                    if ( ((u<.83)&&(u>.73))&&(uprev <.25))
                        {
                            n=n+1;
                            flag =0;
                        }
                    uprev = u;
                    avgout = avgout + u*(double)(flag);
                }
            avgout = avgout/(double)(avgn);
            ofs << avgout<< endl;
        }
    ofs.close();
  return 0;
}
