//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 #include #include using namespace std; //Since vectors are NOT automatically passed by reference //the & is necessary (& v) void read (istream & in, vector & 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 & v) { int sum=0; for (int k=0; k & v) { for (int k=0; k 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; }