// templateMain.cpp

// This program computes the standard deviation of a data set
// that is stored in a separate data file.  It demonstrates
// the use of a linked list to hold the data set in memory.

// Author: Dr. Jeffrey Blessing

#include <iostream>
#include <fstream>
#include <cmath>
//#include <string>
//#include "rational.h"
#include "TemplateList.h"
using namespace std;

int main()
{
	fstream fs("data.in");		// opens the data file
	LinkedList<double> list;	// notice, not list();
//	LinkedList<Rational> rlist;	// Rational data works, but
//	LinkedList<string> strlist;	// string data doesn't run properly
//	because the default value for the data field is the value zero.
	double	data = 0;
	double	sum = 0;
	int		count = 0;

	// read data from file into list, computing average.
	while (fs >> data)
	{
		list.add(data);
		count++;
		sum += data;
	}

	double mean = sum / count;
	double sse = 0;

	// test copy constructor and << operator definition
	for (int j = 1; j < 10; ++j) list.getNext();
	LinkedList<double> list2(list);
	cout << "List 1 = " << list << endl;
	cout << "List 2 = " << list2 << endl;
	cout << "Next element in List 1 is " << list.getNext() << endl;
	cout << "Next element in List 2 is " << list2.getNext() << endl;

	// now test the assignment operator
	LinkedList<double> list3;
	list3 = list;
	cout << "List 3 = " << list3 << endl;

	list.start();		// reset the list to use it again

	// compute the standard deviation
	for (int i = 1; i <= count; i++)
	{
		double next = list.getNext();

		sse += (next - mean) * (next - mean);
	}

	double std = sqrt(sse / (count-1));

	// output the standard deviation
	cout << "Std. Dev. = " << std << endl;

	return 0;
}

