//Goh Yitian A20EC0038
//Hanis Rafiqah Binti Hisham Razuli A20EC0041


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

struct dataAcc
{
	int numAcc[10]; //number of road accidents from 2006-2015
	string state; //states in Malaysia
	float avg; //average number of road accidents from 2006-2015

};

//global constant
const int NUM_STATE = 14;
const int NUM_YEAR = 10;

//global variable
ofstream out("output.txt");


//function prototype
void displayLine();
float cal_Avg(int[]);
void find_HighLow(dataAcc[]);

int main(){

	dataAcc data[NUM_STATE];
	ifstream inputFile;
	inputFile.open("input.txt"); //open input file

	displayLine();
	out<<setw(10)<<"STATE";
	int y=2006;
	out<<setw(14)<<y;
	for (int count=1; count<NUM_YEAR; count++){
		out<< setw(7)<< y+count ;
	}
	out<<setw(10)<<"AVERAGE"<<endl;
	displayLine();

	int s=0;
	while (!inputFile.eof()){
		for (int s=0; s<NUM_STATE; s++){
	
			for (int i=0; i<NUM_YEAR; i++){
				inputFile>>data[s].numAcc[i];
			}
		getline(inputFile,data[s].state);
		}

    }
    
    for (int s=0; s<NUM_STATE; s++){
		out<<left<<setw(17)<<data[s].state;
		for (int i=0; i<NUM_YEAR; i++){
			out<<right<<setw(7)<<data[s].numAcc[i];
	
		}
		out<<fixed<<setprecision(1);
		out<<setw(10)<<cal_Avg(data[s].numAcc)<<endl;
	}
	displayLine();
	find_HighLow(data);
	displayLine();


	inputFile.close();
	out.close();
	return 0;
}

void displayLine(){
	for (int i=0; i<98; i++)
		out << "-" ;
	out<<endl;
}

float cal_Avg(int n[NUM_YEAR]){
	float total=0.0;

	for (int acc=0; acc<NUM_YEAR; acc++){
		total+=n[acc];
	}
	float avg=total/NUM_YEAR;

	return avg;
}

void find_HighLow(dataAcc data[NUM_STATE]){
	int highest=0, hYear;
	string hState;

	for(int i=0; i<NUM_STATE; i++)
	{
		for (int n=0; n<NUM_YEAR; n++)
		{
			if (highest<data[i].numAcc[n])
			{
				highest=data[i].numAcc[n];
				hYear=n+2006;
				hState=data[i].state;
			}
		}
	}
	out<<"The highest number of road accidents = ";
	out<< highest <<" at"<< hState <<" on "<< hYear <<endl;

}


