/*These inclusion and 'using' statements are local. They do not make 
 * the names or packages available to programs calling these functions.
 */

#include "show_and_mode.h"
#include <algorithm>
#include <stdexcept>
#include<iostream>

using std::cout; using std::sort;
using std::domain_error; using std::vector;

typedef vector<int>::size_type vec_size;

void show_vec(const vector<int>& vec) // This is the definition of the function.
{
	
	for(vec_size i=0; i!=vec.size();i++)
		cout<<vec[i]<<" ";
	return;
}

int mode_elt(vector<int> vec)
{
	if(vec.size()!=0)
	{
		sort(vec.begin(),vec.end());
		int max_count=1, count=1, mode=vec[0];
/* Loop invariants: 'count' holds the number of values so far matching vec[i-1].
 * 'max_count' holds the highest number of matches seen so far.
 * 'mode' holds the first value occurring that many times.
 */ 
		for(vec_size i=1; i!=vec.size(); i++)
		{
			
			if(vec[i-1]==vec[i])
			{
				count++;
				if(count>max_count)
				{
					mode=vec[i];
					max_count=count;
				}
			}
			else
				count=1;
		}
		
		
		return mode;
	}
	throw domain_error("mode of an empty vector");//Flag a problem.
}
