//QUESTION 2
//SECP SECTION 2
//29 NOVEMBER 2019
//MOHAMAD AMIN HAZEEQ BIN HISHAM
//000804-01-1523
//IMRAN HAKIM BIN NORASMADI
//001220-03-0223
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void displayLine();
int findInWinner(int winner1,int winner2,int winner3);
int findTeamWinner(int team1,int team2,int team3);

int main()
{
    ifstream inFile;
	inFile.open("input2.txt");
	
	if(inFile.fail())
	{
		cerr<<"Sorry, input file does not exist!"<<endl;
		cerr<<"Press any key to continue . . . "<<endl;
		exit(1);
	}	
	int marks[12][7];
	
	
	
	while(!inFile.eof())
	{
		for(int R=0;R<12;R++)
		{
			for(int C=0;C<7;C++)
			{
				inFile>>marks[R][C];
			}		
	    }
    }
   
	displayLine();
	cout<<endl;
	cout<<setw(9)<<"Id"<<setw(9)<<"E1"<<setw(9)<<"E2"<<setw(9)<<"E3"<<setw(8)<<"E4"<<setw(7)<<"E5"<<setw(8)<<"Total"<<endl;
	displayLine();
	cout<<endl;
	cout<<setw(13)<<"TEAM 1"<<endl;
	int tot=0;
    int team1=0;
	int tot_each1[4];
	int win=0;
	int winner1=0;
	
		for(int R=0;R<4;R++)
			{
			 for(int C=1;C<7;C++)
				{
				  if(C==1)
				    {
				     cout<<setw(11)<<marks[R][C];
				    }
				  else
				    {
				     cout<<setw(8)<<marks[R][C];
				     tot+=marks[R][C];
				     team1+=marks[R][C];
				    }
			    }
			    tot_each1[R]=tot;
			    if(tot_each1[R]>win)
			    {	
	  		    win=tot_each1[R];
	  		    winner1=marks[R][1];
				}
			    cout<<setw(7)<<tot_each1[R]<<endl;
			    tot=0;
			    
			}
			cout<<setw(12)<<"TOTAL"<<setw(46)<<team1<<endl;
			
     
	
	displayLine();
	cout<<endl;
	cout<<setw(13)<<"TEAM 2"<<endl;
	
	int tot2=0;
    int team2=0;
	int tot_each2[4];
	int win2=0;
    int winner2=0;
    
		for(int R=4;R<8;R++)
			{
			 for(int C=1;C<7;C++)
				{
				  if(C==1)
				    {
				     cout<<setw(11)<<marks[R][C];
				    }
				  else
				    {
				     cout<<setw(8)<<marks[R][C];
				     tot2+=marks[R][C];
				     team2+=marks[R][C];
				    }
			    }
			    tot_each2[R]=tot2;
			    if(tot_each2[R]>win2)
			    {
			    	win2=tot_each2[R];
			    	winner2=marks[R][1];
				}
			    cout<<setw(7)<<tot_each2[R]<<endl;
			    tot2=0;
			    
			}
			cout<<setw(12)<<"TOTAL"<<setw(46)<<team2<<endl;

	
	
		displayLine();
	cout<<endl;
	
    cout<<setw(13)<<"TEAM 3"<<endl;
    
    int tot3=0;
    int team3=0;
	int tot_each3[4];
	int win3=0;
	int winner3=0;

    
		for(int R=8;R<12;R++)
			{
			 for(int C=1;C<7;C++)
				{
				  if(C==1)
				    {
				     cout<<setw(11)<<marks[R][C];
				    }
				  else
				    {
				     cout<<setw(8)<<marks[R][C];
				     tot3+=marks[R][C];
				     team3+=marks[R][C];
				    }
			    }
			    tot_each3[R]=tot3;
			    if(tot_each3[R]>win3)
			    {
			    	win3=tot_each3[R];
			    	winner3=marks[R][1];
				}
			    cout<<setw(7)<<tot_each3[R]<<endl;
			    tot3=0;
			    
			}
			cout<<setw(12)<<"TOTAL"<<setw(46)<<team3<<endl;

	
	int findwinner=findInWinner( winner1, winner2, winner3);
	int teamwinner=findTeamWinner(team1,team2,team3);
	int group=0;
	if(findwinner==winner1 )
	{
		group=1;
    }
    if(findwinner==winner2 )
    {
    	group=2;
	}
	if(findwinner==winner3 )
	{
		group=3;
	}
	displayLine();	

	cout<<endl<<endl;
	cout<<"       Winner for Individual Category: "<<findwinner<<" (Team "<<group<<")"<<endl;
	
	int gteam=0;
	if(teamwinner==team1 )
	{
		gteam=1;
	}
	if(teamwinner==team2 )
	{
		gteam=2;
	}
	if(teamwinner==team3)
	{
		gteam=3;
	}
	
	cout<<"       Winner for Group Category: Team "<<gteam<<" (Score = "<<teamwinner<<" ) "<<endl;	
    
    inFile.close();

	
	return 0;
}

void displayLine()
{
	cout<<setw(8);
	for(int k=0;k<52;k++)
	{
		cout<<'-';
	}
}
int findInWinner(int winner1,int winner2,int winner3)
{
	int individu=winner1;
	if(winner2>individu)
	{
		individu=winner2;
	}
	if(winner3>individu)
	{
		individu=winner3;
    }
    
    return individu;
    	
	
}
int findTeamWinner(int team1,int team2,int team3)
{
	int score=team1;
	if(team2>score)
	{
		score=team2;
	}
	if(team3>score)
	{
		score=team3;
	}
	
	return score;
}
