
/*	SHAKIRAH BINTI MOHD SHUKUR (A19EC0159)
	AINA ALIAH BINTI RUSLAN (A19EC0012)	 */

#include<iostream>
#include<fstream>
#include<iomanip>
#define NUM_STATE 14
#define NUM_YEAR 10
using namespace std ;

ofstream out ("output.txt") ;

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_Avg (int A[])
{
	int total = 0 ;
	float average ;
	
	for (int i = 0; i < NUM_YEAR; i++)		//kira total
		total += A[i] ;
	
	average = total / (float)NUM_YEAR ;		//kira average 
	
	return average ;
}

void find_Highlow (dataAcc A[], int year[])
{
	int high = 0, index = 0, y ;
	
	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] ;	//nak dapatkan high
				index = i ;				//index untuk state
				y = j ;				    //tahun
			}
		}
	}
	
	out << "The highest number of road accidents = " << high << " at " << A[index].state << " on " << year[y];
	out << endl ;
}

int main ()
{
	dataAcc A[NUM_STATE] ;
	int y[] = {2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015} ;  //2006-2015
	
	ifstream inp ;
	
	inp.open ("input1.txt") ;
	if ( ! inp.is_open() )
	{
		out << "ERROR !!" << endl ;
		exit (0) ;
	}	
	
	for (int i = 0; i < NUM_STATE; i++)   //baca dari input1.txt
	{
		for (int j = 0; j < NUM_YEAR; j++)
			inp >> A[i].numAcc[j] ;
		getline (inp, A[i].state) ;
	}
	
	//output
	displayLine () ;	//call function
	out << endl ;
	
	out << left ;
	
	out << setw(19) << " STATE" ;
	
	for (int l = 0; l < NUM_YEAR; l++) 		//display tahun
		out << setw(7) << y[l] ;
			
	out << setw(10) << "AVERAGE" << endl ;
	out << endl ;
	displayLine () ;	//call function
	
		
	for (int x = 0; x < NUM_STATE; x++) //display state 
	{	
		out << setw (19) << A[x].state ;
		
		for (int n = 0; n < NUM_YEAR; n++)		
			out << setw(7) << A[x].numAcc[n] ;	//display data
		
		A[x].avg = cal_Avg (A[x].numAcc) ;	//call function 
		out << fixed << setprecision (1) << setw(6) << A[x].avg ; 	//display average
		
		out << endl << endl ;
	}

	out << endl ;
	displayLine () ;	//call function
	out << endl ;	
	
	find_Highlow (A, y) ;
	
	out << endl ;
	displayLine () ;	//call function
	
	return 0 ;
}
