// School of Computing, Universiti Teknologi Malaysia
// SECJ1013- Programming Technique I
// Semester 1, 2020/2021
// ASSIGNMENT IV
// 27 Jan 2021
// Pair information 
// Name: Gui Yu Xuan
// Matrics No: A20EC0039 
// Name:
// Matrics No:
// Section: 01

#include<iostream>
#include<iomanip>
#include<fstream>

using namespace std;

const int NUM_STATE=14; //declared global constant of number of states in Malaysia
const int NUM_YEAR=10; //declared global constant of number of years 
ofstream out; //declare a file variable for output file


// starting a struct function of dataAcc
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	
};  // end of struct function


void displayLine(); //function to display lines
float cal_Avg(dataAcc[],int); //function to calculate the average number of road accidents for each state
void find_HighLow(dataAcc [],int); //function to find and display the highest number of road accidents from 2006-2015


//starting main function
int main()
{
	int counter=0; //declare variable counter to 0
	
	dataAcc data[50]; //declare variable struct dataAcc 
	
	ifstream inp; //declare a file variable for input file
	inp.open("input.txt"); //open an input file named input.txt
	out.open("output.txt"); //open an output file named output.txt
	
	
	//starting a while statement
	while(!inp.eof()) //read the input until the end of input file
	{
		for(int i=0;i<NUM_YEAR;i++) 
		{
			inp>>data[counter].numAcc[i]; //read the number of accidents in input file
		
		}
		
		getline(inp,data[counter].state); //read the states in input file
		
		counter++; //increment counter by 1
	} //end of while statement
	
	
	displayLine(); //call function displayLine
	
	
	out<<setw(10)<<"STATE";
	out<<"\t";
	for(int i=0;i<10;i++)
	{
		out<<setw(7)<<2006+i;
	
	}
	out<<setw(10)<<"AVERAGE"<<endl;
	
	displayLine(); //call function 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];
			
		}
		out<<fixed<<setprecision(1);
		out<<setw(10)<<right<<cal_Avg(data,i)<<endl; //call function cal_Avg
	}
	
	displayLine(); //call function displayLine
	
	find_HighLow(data,NUM_STATE); //call function find_HighLow
	
	displayLine(); //call function displayLine
	
	inp.close(); //close the input file
	out.close(); //close the output file
} //end main function


// function to display lines using 98 characters of '-' in the output file using a loop
void displayLine()
{
	for(int i=0;i<98;i++)
	{
		out<<"-";
	}
	
		out<<endl;
}


//calculate the average number of road accidents for each state
//return the average number of road accidents for each state
float cal_Avg(dataAcc data[], int n)
{
	float sum=0; //declare variable sum 
	
	for(int i=0;i<NUM_YEAR;i++)
	{
		sum+=data[n].numAcc[i];	
	}
	
	return (sum/NUM_YEAR);
	
}


//the function display the highest number of road accidents form 2006 to 2015
void find_HighLow(dataAcc data[],int NUM_STATE)
{
	//declare variable
	int high=0,low=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]>high)
			{
				high=data[i].numAcc[j];
				highYear=j+2006;
				highState=data[i].state;
			}
			
			if(data[i].numAcc[j]<low)
			{
				low=data[i].numAcc[j];
				lowYear=j+2006;
				lowState=data[i].state;
			}
		}
	}
	
	out<<"The highest number of road accidents = "<<high<<" at"<<highState<<" on "<<highYear<<endl;
}


