//This version of the vector average problem exploits the ability of
//vectors to monitor their own size through the size() method.
//This means that we can drop the third parameter, n, in each of
//the following functions

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

//Since vectors are NOT automatically passed by reference
//the & is necessary (& v)
void read (istream & in, vector<int> & v)
{
	int number;
	int n=0;	//LOCAL VARIABLE NOW
	while (in>>number)
	{
		v.resize(++n);	//Vectors allow dynamic sizing of the list!
		v[n-1]=number;
	}
}

//The advantages of pass by reference,
//namely, the economic use of memory,
//together with the safety of pass by value
//can be achieved by the joint use of const and &
int average (const vector<int> & v)
{
	int sum=0;
	for (int k=0; k<v.size(); k++)
	{
		sum+=v[k];
	}
	return sum/v.size();
}

void write (ostream & out, const vector<int> & v)
{
	for (int k=0; k<v.size(); k++)
	{
		out << v[k] << endl;
	}
}

int main()
{
	vector<int> v;
	ifstream fin ("list.txt");
	ofstream fout ("list_out.txt");
	//int n		NOT NECESSARY ANYMORE
	
	read (fin, v);
	write (fout, v);
	
	fout << "The average of the " << v.size() << " items in the list is: "
		<< average (v) << endl;
		
	return 0;
}

