// Tan Ming Hui A20EC0155
// Kalaivani A/P Subramaniam A20EC0056
// 2021/01/21
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

//function prototype 
void displayLine ();
int *findIndWinner (int [],int [][7],int);
int *findTeamWinner(int [],int [][7],int);

int main ()
{
	// decleare 
	const int r=12,c=7;
	int marks [r] [c];
	string header [c]={"Id","E1", "E2" ,"E3","E4","E5","Total"};
	int totalInd [r];
	int totalGrp [3];
	int total,a,*teamwin,*indwin;
	ifstream input ("input2.txt"); //open the input file
	
	if (!input.is_open())
	{
		cout << "Sorry, input file does not exist! " << endl;
		cout << "Press any key to continue . . ." << endl;
		exit (1);
	}
	
	// read the data in file and copy to array
	while (!input.eof())
	{
		for (int r=0;r<12;r++)
		{
			for (int c=0;c<7;c++)
			{
				input >> marks[r] [c];
			}
		}
	}
	
	// total up each of the event marks for respective participant
	for (int r=0;r<12;r++)
	{
		for (int c=2;c<7;c++) // calc sum of the score of five event that store in the array start from index 2
		{
			total=total+marks[r][c];
		}
		totalInd[r] = total;
		total=0; // set the total back to null before continue for the next participant
	}
	
	// calc total score for each team
	for (a=0;a<3;a++)
	{
		if (a==0)
		totalGrp[a] = totalInd[0]+totalInd[1]+totalInd[2]+totalInd[3];
		else if (a==1)
		totalGrp[a] = totalInd[4]+totalInd[5]+totalInd[6]+totalInd[7];
		else 
		totalGrp[a] = totalInd[8]+totalInd[9]+totalInd[10]+totalInd[11];
	}
	indwin = findIndWinner(totalInd,marks,r);
	teamwin = findTeamWinner(totalGrp,marks,r);

	
	// output of program
	displayLine ();
	for (int i=0;i<7;i++)
	{
		cout<<left<<setw(7)<< header[i];
	}
	displayLine ();	
	
	//for first team 
	cout<<"TEAM "<<marks[0][0]<<endl;
	for (int r=0;r<4;r++)
	{
		for (int c=1;c<7;c++)
		{
			cout<<left<<setw(7)<<marks[r][c];
		}
		cout << totalInd[r] << endl;	
	}
	cout<<"TOTAL" <<setw(40)<<right<<totalGrp[0];
	displayLine();
	
	//for second team 
	cout<<"TEAM "<<marks[4][0]<<endl;
	for (int r=4;r<8;r++)
	{
		for (int c=1;c<7;c++)
		{
			cout<<left<<setw(7)<<marks[r][c];
		}
		cout << totalInd[r] << endl;	
	}
	cout<<"TOTAL" <<setw(40)<<right<<totalGrp[1];
	displayLine();
	
	//for third team 
	cout<<"TEAM "<<marks[8][0]<<endl;
	for (int r=8;r<12;r++)
	{
		for (int c=1;c<7;c++)
		{
			cout<<left<<setw(7)<<marks[r][c];
		}
		cout << totalInd[r] << endl;	
	}
	cout<<"TOTAL" <<setw(40)<<right<<totalGrp[2];
	displayLine();

	cout<<endl<<endl;
	cout << "Winner for Individual Category: " << indwin[0] << " (Team "<< indwin[1] <<")" <<endl;
	cout << "Winner for Group Category: Team " << teamwin[0]<<" (Score = " << teamwin[1]<<")";
	cout << "\n\nPress any key to continue . . ." <<endl;  
	input.close(); // close input file
	return 0;	
}

void displayLine()
{
	cout<<endl;
	for (int i=0;i<52;i++)
		cout << "-";
	cout<<endl;
}

int *findTeamWinner(int arr[],int m[][7],int r)
{
	int highest=0,*winTeamInfo,team,index;
	winTeamInfo = new int [2];
	for (int i=0;i<3;i++)
	{
		if (arr[i]>highest)
		{
			highest=arr[i];
			index=i;
		}
			if (index==0)
			team= m[0][0];
			else if(index==1)
			team=m[4][0];
			else
			team=m[8][0];
	}
	winTeamInfo[0]=team;
	winTeamInfo[1]=highest;
	return winTeamInfo;
	// free dynamic memory and point to 0
	delete []winTeamInfo;
	winTeamInfo=0;
}

int *findIndWinner (int array[],int n[][7],int c)
{
	int highest=0,*winIndInfo,id,team;
	winIndInfo = new int [3];
	for (int i=0;i<12;i++)
	{
		if (array[i]>highest)
		{
			highest=array[i];
			id=i;	// to hold the index
		}
		if ((id>=0)&&(id<4))
			team=n[0][0];
		else if ((id>=4)&&(id<8))
			team=n[4][0];
		else 
			team=n[8][0];
	}
	
	winIndInfo[0]=n[id][1];
	winIndInfo[1]=team;
	winIndInfo[2]=highest;
	return winIndInfo;
	// free dynamic memory and point to 0
	delete []winIndInfo;
	winIndInfo=0;
}
