#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

const int NUM_STATE=14;
const int NUM_YEAR=10;
ofstream *out;

struct dataAcc
{
	int numAcc[10]; //number of road accidents from 2006-2015
	string state;   //states in Malaysia
	float avg;      //average number of road accidents for 2006-2015
};

void displayLine()
{
	for (int i=0; i<98; i++)
		*out<< "-";
	*out << endl;
}

float Cal_Avy(dataAcc s[], int count)
{
	float sum=0.0,avg;
	for (int i=0;i<NUM_YEAR;i++)
	{
		sum=sum+s[count].numAcc[i];
	}
	avg=sum/NUM_YEAR;
	return avg;
}

int find_HighLow(dataAcc s[],int NUM_STATE)
{
	int highest=0;
	for (int count=0;count<NUM_STATE;count++)
	{
		for (int i=0;i<NUM_YEAR;i++)
		{
			if(highest<s[count].numAcc[i])
				highest=s[count].numAcc[i];
		}
	}	
	return highest;
} 

int main ()
{
	ifstream infile;
	ofstream outfile;
	out=&outfile;
	
	infile.open("input.txt");
	outfile.open("output.txt");
	
	int num,h,year;
	float avg; 
	dataAcc s[NUM_STATE];
	string hs;
	
	if (infile.fail())
	{
		cout<<"ERROR : Cannot open file\n";
		exit (1);
	}
	
	displayLine();
	*out<<setw(10)<<"STATE"<<"\t";
	for (int i=2006;i<=2015;i++)
	{
		*out<<setw(7)<<right<<i;
	}
	*out<<setw(11)<<"AVERAGE\n";
	displayLine();
	
	while(!infile.eof())
	{
		for(int count=0;count<NUM_STATE;count++)
		{
			for(int i=0;i<NUM_YEAR;i++)
			{
				infile>>s[count].numAcc[i];
			}
			
			*out<<setw(16)<<left;
			getline(infile,s[count].state);
			*out<<s[count].state;
			
			for(int i=0;i<NUM_YEAR;i++)
			{
				*out<<setw(7)<<right<<s[count].numAcc[i];
			}
			
			avg=Cal_Avy(s,count);
			*out<<setw(10)<<fixed<<showpoint<<setprecision(1)<<avg<<endl;
			
		}
		h=find_HighLow(s,NUM_STATE);
	}
	
	for (int count=0;count<NUM_STATE;count++)
	{
		for (int i=0;i<NUM_YEAR;i++)
		{
		if(h==s[count].numAcc[i])
		{
			hs=s[count].state;
			switch(i)
			{
				case 0: year=2006;
						break;
				case 1: year=2007;	
						break;
				case 2: year=2008;	
						break;
				case 3: year=2009;	
						break;
				case 4: year=2010;	
						break;
				case 5: year=2011;	
						break;
				case 6: year=2012;	
						break;
				case 7: year=2013;	
						break;					
				case 8: year=2014;	
						break;
				case 9: year=2015;	
						break;
				default : break;
			}
		}
		}
	}
			
	displayLine();
	*out<<"The highest number of road accidents = ";	
	*out<<h<<" at"<<hs<<" on "<<year<<endl;
	displayLine();
	
	return 0;
	
}
