////////////////////////////////////////////////////////////////////////////////
// School of Computing, Universiti Teknologi Malaysia
// SECJ1013- Programming Technique I
// Semester 1, 2020/2021
// ASSIGNMENT IV
// 31/1/2021
// Pair information
//
// Name: MUHAMMAD AIMAN BIN ABDUL RAZAK
// Matrics No: A20EC0082
// Section: 01
//
// Name: HEONG YI QING
// Matrics No: A20EC0043
// Section: 01
//
// Name: CHONG KAH WEI
// Matrics No: A20EC0027
// Section: 01
////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

const int NUM_STATE=14; //global constant NUM_STATE
const int NUM_YEAR=10;  //global constant NUM_YEAR

ofstream out("output.txt"); //global variable out

struct dataAcc //struct dataAcc definition
{
	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(); //prototype for displayLine() function
float cal_Avg(int []); //prototype for cal_Avg() function
void find_HighLow(dataAcc []); //prototype for find_HighLow()

int main() //main() function
{
	dataAcc allstates[NUM_STATE]; //declaration for allstates variable type dataAcc
	ifstream in("input.txt"); //in is a varibale to point to input file named "input.txt"
	
	if (in.fail())
	{
		cout<<"Error opening the file! Ending...";
		exit(EXIT_FAILURE);
	}
	
	displayLine();
	out<<right<<setw(10)<<"STATE";
	out<<right<<setw(14)<<"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(10)<<"AVERAGE"<<endl;
	displayLine();

    while (!in.eof()) //reading from "input.txt"...
	{
		for (int i=0; i<NUM_STATE; i++)
		{
		    for (int j=0; j<NUM_YEAR; j++)
		    {
			    in>>allstates[i].numAcc[j];
		    }
		    getline(in, allstates[i].state);
	    }
	}
	
	for (int i=0; i<NUM_STATE; i++) //writing to "output.txt"...
	{
		out<<left<<setw(17)<<allstates[i].state;
		for (int j=0; j<NUM_YEAR; j++)
	    {
	    	out<<right<<setw(7)<<allstates[i].numAcc[j];
		}
		out<<fixed<<setprecision(1)<<right<<setw(10)<<cal_Avg(allstates[i].numAcc)<<endl;
	}
	
	displayLine();
	find_HighLow(allstates); //calling to find_HighLow() function
	displayLine();
	
	in.close(); //close in
	out.close(); //close out
}

void displayLine() //displayLine() function
{
	for (int i=0; i<98; i++)
	    out<<"-";
	out<<endl;
}

float cal_Avg(int numAcc[]) //cal_Avg() function
{
	int sum=0;
	float accavg=0;
	
	for (int i=0; i<NUM_YEAR; i++)
	{
		sum+=numAcc[i];
	}
	
	accavg=(sum*1.0)/NUM_YEAR;
	
	return accavg;
}

void find_HighLow(dataAcc allstates[]) //find_HighLow() function
{
	int highest=0;
	int highestyear=0;
	string higheststate;
	
	for (int i=0; i<NUM_STATE; i++)
	{
	    for (int j=0;j<NUM_YEAR; j++)
	    {
		    if (allstates[i].numAcc[j]>highest)
		    {
		        highest=allstates[i].numAcc[j];
		        highestyear=j;
		        higheststate=allstates[i].state;
	        }
	    }
    }
	out<<"The highest number of road accidents = "<<highest<<" at "<<higheststate<<" on "<<(2006+highestyear)<<endl;
}
