////////////////////////////////////////////////////////////////////////////////
// School of Computing, Universiti Teknologi Malaysia
// SECJ1013- Programming Technique I
// Semester 1, 2020/2021
// ASSIGNMENT IV
// 23 Jan 2021
// Pair information
//
// Name: Marnisha binti Mustafa Kamal
// Matrics No: A20EC0075
// Section: 01
//
// Name: Woon Zi Jian
// Matrics No: A20EC0171
// Section: 01
//

#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

struct dataAcc
{
	int numAcc[10]; //number of road accident
	string state; //states in Malaysia
	float avg; //average number of road accident
};

const int NUM_STATE=14;
const int NUM_YEAR=10;
ofstream out ("output.txt");

void displayLine();
float calc_Avg(dataAcc line);
int find_HighLow(dataAcc total[]);

int main()
{
	int counter=0;
	ifstream inputfile;
	inputfile.open("input.txt");
	displayLine();
	out<<setw(10)<<"STATE"<<setw(15)<<"2006"<<setw(8)<<"2007"<<setw(8)<<"2008"<<
	setw(8)<<"2009"<<setw(8)<<"2010"<<setw(8)<<"2011"<<setw(8)<<"2012"<<
	setw(8)<<"2013"<<setw(8)<<"2014"<<setw(8)<<"2015"<<setw(10.5)<<"AVERAGE"<<endl;
	dataAcc line;  
	dataAcc total[NUM_STATE];  
	displayLine();
	
	
	 
	 while(!inputfile.eof())
	 {
	 	for(int i=0;i<NUM_YEAR;i++)
			{
				inputfile>>line.numAcc[i];
			}
			
			getline(inputfile,line.state,'\n');
			out<<setw(18)<<left<<line.state;
		
			for(int i=0;i<NUM_YEAR;i++)
			{
				out<<right<<setw(7)<<line.numAcc[i]<<" ";
			}
			
			line.avg=calc_Avg(line);
			out<<fixed<<setprecision(1)<<showpoint<<setw(9)<<line.avg<<endl;
			
			total[counter]=line; 
			counter++; 
	 }
	 
	displayLine();
	find_HighLow(total);
}

void displayLine()
{
	for(int i=0;i<108;i++)
	out<<"-";
	out<<endl;
}

float calc_Avg(dataAcc line)
{
	float avg=0;
	int total=0;
	for(int i=0;i<NUM_YEAR;i++)
	total+=line.numAcc[i];
	avg=total/10.0;
	return avg;
}

int find_HighLow(dataAcc total[])
{
	int highest=0;
	string highestState;
	int highestYear=0;
	for(int i=0;i<NUM_STATE;i++)
	{
		for(int j=0;j<NUM_YEAR;j++)
		{
			if(total[i].numAcc[j]>highest)
			{
				highest=total[i].numAcc[j];
				highestState=total[i].state;
				highestYear=2006+j;
			}
		}
	}
	out<<"The highest number of road accidents = "<<highest<<" at"<<highestState<<" on "<<highestYear<<endl;
	displayLine();
	
}
