/*Name : SHASITHER A/L SANDRAN
  I/C  : 000111-10-0515
  Name : NALINI A/P VIJAYAN
  I/C : 001223-10-1330
  Section : 2
  Date : 21/12/2019*/

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#define NUM_STATE 14
#define NUM_YEAR 10

ofstream out("output.txt");

struct dataAcc
{
	int numAcc[10]; //number of rod accidents
	string state;  // name of Malaysia states
	float avg;    //average number of road accidents
};

void displayLine()
{
  for (int i = 0; i < 98; i++)
    out << "-";
    out << endl;}
    
//Function to calculate the average for each state
float cal_Avg(int accident[NUM_YEAR])  
{
	int total = 0;
	for(int i=0; i<NUM_YEAR; i++)
		total += accident[i];
		
	return total/static_cast<float>(NUM_YEAR);
}

//Function to calculate highest road accidents
void find_HighLow(dataAcc accident[NUM_STATE])
{
	int max = accident[0].numAcc[0], ind, year;
    for (int i = 0; i < NUM_STATE; i++){
      for (int j = 0; j < NUM_YEAR; j++){
         if (accident[i].numAcc[j] > max){
          max = accident[i].numAcc[j];
          ind = i;
          year = j; }}}
          
    out << "The highest number of road accidents = " << max << " at" << accident[ind].state << " on " << 2006 + year << endl; 
}

int main(){
  ifstream inp("input1.txt");
  dataAcc accident[NUM_STATE]; //array of struct
  
  //Input data from input1.txt file
  for(int i=0; i<NUM_STATE; i++){
  	for(int j=0; j<NUM_YEAR; j++)
  	  inp >> accident[i].numAcc[j];
  	  getline(inp, accident[i].state);
  }
  
  //Print output on output.txt file
  displayLine();
  out << setw(10) << "STATE" << setw(14);
   for (int i = 2006; i <= 2015; i++)
     out << i << setw(7);
     out << setw(10) << "AVERAGE" << endl;
  displayLine();
  
  
  for (int i = 0; i < NUM_STATE; i++){
    out << left << setw(17) << accident[i].state << right;
    accident[i].avg = cal_Avg(accident[i].numAcc);
  for(int j = 0; j < NUM_YEAR; j++) 
    out << setw(7) << accident[i].numAcc[j]; 
    out << setw(10) << fixed << setprecision(1) << accident[i].avg << endl;}
    
  displayLine(); 
  find_HighLow(accident); //Call function to find highest
  displayLine(); 


  out.close();
  return 0;
}


