////////////////////////////////////////////////////////////////////////////////
// School of Computing, Universiti Teknologi Malaysia
// SECJ1013- Programming Technique I
// Semester 1, 2020/2021
// ASSIGNMENT IV
// 30 Jan 2021
// Pair information 
// Name: Phang Cheng Yi   &&  Shahril bin Saiful Bahri
// Matrics No: A20EC0131  &&  A20EC0144
// Section: 01
//
////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

//Global constants
const int NUM_STATE=14;  //Number of states
const int NUM_YEAR=10;   //Number of years
//Global variable
ofstream out; 

struct dataAcc
{
	int numAcc[10]; //number of road accidents from 2006 to 2015
	string state;   //state in Malaysia
	float avg;      //average number or road
};

void displayLine();
double cal_Avg(dataAcc[],int);
void find_HighLow(dataAcc[]);

int main()
{
	int count=0;
	float avg;
	dataAcc data[30];
	
	ifstream inp;
	inp.open("input.txt");
	out.open("output.txt");
	
	while(!inp.eof())
	{
		for(int i=0;i<NUM_YEAR;i++)
		{
			inp>>data[count].numAcc[i];  //reading the numbers of road accidents from file
		}
		
		getline(inp,data[count].state);  //reading the state name from file
		
		count++;
	}
	
	displayLine();
	
	out<<setw(11)<<"STATE\t";
	for(int i=0;i<NUM_YEAR;i++)
	{
		out<<setw(7)<<2006+i;
	}
	
	out<<setw(10)<<"AVERAGE"<<endl;
	
	
	displayLine();
	
	for(int i=0;i<NUM_STATE;i++)
	{
		out<<setw(16)<<left<<data[i].state;
		
		for(int j=0;j<NUM_YEAR;j++)
		{
			out<<setw(7)<<right<<data[i].numAcc[j];
		}
		
		avg=cal_Avg(data,i);
		out<<fixed<<setprecision(1);
		out<<setw(10)<<right<<avg<<endl;
	}
	
	displayLine();
	
	find_HighLow(data);
	
	displayLine();
	
	inp.close();
	out.close();
	
	return 0;

}

void displayLine()
{
	for(int i=0;i<98;i++)
	out << "-";
	out <<endl;
}

double cal_Avg(dataAcc data[],int n)
{
	double sum;
	for(int i=0;i<NUM_YEAR;i++)
	{
		sum += data[n].numAcc[i];
	}
	
	return (sum/NUM_YEAR);
	
}

void find_HighLow(dataAcc data[])
{
	int highest=0, lowest=999999;
	int highYear, lowYear;
	string highState, lowState;
	
	for(int i=0;i<NUM_STATE;i++)
	{
		for(int j=0;j<NUM_YEAR;j++)
		{
			if(data[i].numAcc[j]<lowest)
			{
				lowest=data[i].numAcc[j];
				lowYear=2006+j;
				lowState=data[i].state;
			}
			
			else if(data[i].numAcc[j]>highest)
			{
				highest=data[i].numAcc[j];
				highYear=2006+j;
				highState=data[i].state;
			}
		}
	}
	
	out<<"The highest number of road accidents = "<<highest<<" at "<<highState<<" on "<<highYear<<endl;
}
