////////////////////////////////////////////////////////////////////////////////
// School of Computing, Universiti Teknologi Malaysia
// SECJ1013- Programming Technique I
// Semester 1, 2020/2021
// ASSIGNMENT IV
// 27 Jan 2021
// Pair information 
// Name: Felicia Chin Hui Fen
//     : Mohd Firdaus Bin Zamri
// Matrics No: A20EC0037
//           : A20EC0080
// Section: 01
//
////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

//declare global constants
const int NUM_STATE = 14; //number of states
const int NUM_YEAR = 10; //number of years

ofstream out; //declare a global variable for the output file

struct dataAcc
{
	int numAcc[NUM_YEAR]; //number of road accident from 2006-2015
	string state; //states in Malaysia
	float avg; //average number of road accidents for 2006-2015
};

//function prototype
void displayLine ();
float cal_Avg(int numAcc[]);
void find_HighLow(dataAcc[]);

int main()
{
	dataAcc data[NUM_STATE]; //declare a sturcture variable that use array
	ifstream infile ; //declare variable for the input file

	infile.open("input.txt"); //open the input file

	if (! infile.is_open()) //check the input file is open or not
	{
		cout << "Error. File does not exist! Terminating! \n";
		exit (1); //the program terminate when the input file is not found
	}
	
	//use loop to store the data from the input file in the array of struct
	for (int i=0; i < NUM_STATE; i++ )
	{		
		for (int j=0; j < NUM_YEAR; j++)	
		infile >> data[i].numAcc[j];
	
		getline(infile, data[i].state); 
		
	}
	
	//close the input file
	infile.close();
	
	//store the calculated average from 2006-2015 into the array
	for (int i=0; i<14; i++)
	data[i].avg = cal_Avg(data[i].numAcc); //call function cal_Avg to calculate the average	
	
	out.open("output.txt"); //open the output file
	
	//call function displayline()
	displayLine ();
	
	//display header in the output file
	out << setw(12) << "STATE" <<"\t";
	for (int i=0; i < 10; i++)
	out << setw(7) << right << 2006 + i ;	
	out << setw(10) << "AVERAGE" << endl;
	
	//call funstion displayLine()
	displayLine ();

	//use loop to diplay the data in the output file
	for (int i = 0; i < NUM_STATE; i++)
	{
		out << setw(17) << left << data[i].state;
	
		for (int j = 0; j < NUM_YEAR; j++)
		out << setw(6) << right << data[i].numAcc[j] <<" "; 
	
		out << fixed << showpoint << setprecision(1); //formating the output into one decimal places
		out << setw(9) << right << data[i].avg;
		out<< endl;
		
	}
	
	//call function displayLine()
	displayLine ();
	
	//Call fucntion find_HighLow()
	find_HighLow (data);
	
	//call function diaplayLine()
	displayLine ();

	//close the output file
	out.close();

}

//fuction definition
//display lies using 98 characters of '-' in the output file using a loop
void displayLine ()
{
	for (int i=0; i < 98; i++)
	out << "-";
		
	out << endl;
	
}

//function definition
//calculate the average number of road accidents for each state
//the function will return the average number of road accidents for each state
float cal_Avg(int numAcc[])
{
    float sum = 0.0; //declare variable for sum of the number of road accidents from 2006-2015
    float total_avg; //declare variable to store the total average of the number of road accidents from 2006-2015
    
    //calculate the sum of number of road accidents from 2006-2015
    for (int i=0; i< NUM_YEAR; i++)		
    sum += numAcc[i];

	//calculate the average the number of road accidents occur from 2006-2015
    total_avg = sum / NUM_YEAR; 
	
	return total_avg;

}

//function definition
//the function display the highest number of road accidents form 2006 to 2015
void find_HighLow(dataAcc data[])
{
	//declare variables for the highest number of raod accidents, happen in which state
	//and happen in which year
	int highest = data[0].numAcc[0]; 
	string highest_state = data[0].state; 
	int highest_year;
	
	for (int i=0; i < NUM_STATE ; i++)
	{
		for(int j=0; j < NUM_YEAR; j++)
		{
			if(data[i].numAcc[j] > highest)
			{
				highest = data[i].numAcc[j];
				highest_state = data[i].state;
				highest_year = 2006 + j;
			}
		}		
	}
		//display the highest number of road accidents, its state and its year in the output file
		out << "The highest number of road accidents = " << highest			
		<< " at" << highest_state << " on " << highest_year << endl;
}


