// iterMain.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,
// and the use of a simple iterator type for traversing the
// list container.

// Author: Dr. Jeff Blessing

#include <iostream>
#include <fstream>
#include <cmath>
#include "LinkedListIter.h"
using namespace std;

void main()
{
	fstream fs("data.in");		// opens the data file
	LinkedList list;			// notice not list();
	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 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 list3;
	list3 = list;
	cout << "List 3 = " << list3 << endl;

	// test the iterator implementation
	list3.start();
	LinkedListIter it(list3);

	cout << "List 3 is now: ";
	for (it = list3.begin(); it != list3.end(); ++it)
		cout << *it << ", ";
	cout << 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;
}