//ASSIGNMENT 3
//SECP SECTION 2
//21 DECEMBER 2019
//MOHAMAD AMIN HAZEEQ BIN HISHAM
//000804-01-1523
//IMRAN HAKIM BIN NORASMADI
//001220-03-0223
#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>
#define NUM_STATE  14
#define NUM_YEAR  10
using namespace std;


struct dataAcc
{
	int numAcc[NUM_YEAR]; // number of road accidents from 2006-2015
	string state; //states in Malaysia 
	float avg; //average number of road accidents for 2006-2015
};
void displayLine();
float calcAvg(int A[]);

void find_HighLow(dataAcc A[]);

ofstream out;


int main(){
	
	dataAcc A[NUM_STATE];
	ifstream inp;
	inp.open("input1.txt");
	out.open("output3.txt");
	if(!inp){
		cout << "Error! " << endl;
		exit(0);
		
	}
	for(int i = 0 ; i < NUM_STATE ; i++){
		
		for (int j = 0 ; j < NUM_YEAR ; j++){
			
			inp >> A[i].numAcc[j];
	}
		getline(inp,A[i].state);
	}
	for(int a = 0 ; a < NUM_STATE ; a++){
		A[a].avg = calcAvg(A[a].numAcc);
	}
	

	displayLine();
	out <<endl;
	
	out<<setw(11)<<"STATE"<<setw(15)<<"2006"<<setw(7)<<"2007"<<setw(7)<<"2008"<<setw(7)<<"2009"<<setw(7)<<"2010"<<setw(7)
	<<"2011"<<setw(7)<<"2012"<<setw(7)<<"2013"<<setw(7)<<"2014"<<setw(7)<<"2015"<<setw(9)<<"AVERAGE"<<endl<<endl;
	displayLine();
	
	for(int z=0; z < NUM_STATE ; z++)
	{
		out << left ;
		out << setw(19);
		out<<A[z].state;
		
		out << right ;
		out<<setw(7);
		for(int y=0; y < NUM_YEAR ; y++)
		{
			 
			 out<<A[z].numAcc[y]<<setw(7);
		}
		out << fixed << setprecision(1);
	    out <<setw(9) << A[z].avg << endl << endl;
	}
	
	find_HighLow(A);
	displayLine();
	
	inp.close();
	out.close();
	
	return 0;
}

void displayLine()
{
	for(int i = 0 ; i < 98 ; i++)
	out << "-" ;
	out << endl;
}

float calcAvg(int A[])
{
int total = 0;
for( int i = 0 ; i < NUM_YEAR ; i++)
{
	total += A[i];
}	
return total / (float) NUM_YEAR;
}

void find_HighLow(dataAcc A[])
{
	int high = A[0].numAcc[0], y = 0 , s = 0;
for (int i = 0 ; i < NUM_STATE ; i ++){
	for (int j = 0 ; j < NUM_YEAR ; j++){
		if(A[i].numAcc[j] > high){
		
		high = A[i].numAcc[j];
		s = i;
		y = 2006 + j;
		
	}
	}
} 
displayLine();
	out << endl;
	out << "The highest number of road accidents = " << high << " at " << A[s].state << " on " << y;
	out << endl << endl;
} 
