/*	Muhammad Anas Alif Shah Bin Azeli Shah
	001102-10-0219
	Section 2
	
	Muhammad Irfan Daniel Bin Abd Karim
	000717-10-0667
	Section 2
	
	Date of submission : 30 November 2019 */

#include <iostream>
#include <fstream>
#include <iomanip>
#define r 12
#define c 7
using namespace std;

void displayLine()
{
	for (int i=0; i<52; i++)
	cout << "-";
}

int findIndWinner(int total[], int &a, int &b)
{
	int high=-999;
	for (int i=0; i<r; i++)
	{
		if(total[i] > high)
		{
			high=total[i];
			a=i;
			if(a<4)
			b=1;
			if(a<8)
			b=2;
			if(a<12)
			b=3;
		}
	}
	return high;
}

int findTeamWinner(int totalteam[], int &d)
{
	int hight=-999;
	
	for (int i=0; i<3; i++)
	{
		if (totalteam[i] > hight)
		{
			hight= totalteam[i];
			d=i+1;
		}
	}
	return hight;
}

int main()
{
	int marks[r][c], total[r];
	int totalteam[3]={0, 0, 0};
	int indwinner, teamwinner, a, b, d;
	
	//input start
	ifstream inp("input2.txt");
	
	if (!inp)
	{
		cout << "Sorry, input file does not exist!" << endl;
		cout << "Press any key to continue..." << endl;
		exit(0);
	}
	
	while (!inp.eof())
	{
		for (int i=0; i<r; i++)
		{
			for (int j=0; j<c; j++)
			{
				inp >> marks[i][j];
			}
		}
	}
	//end of input
	
	//plus ind marks
	for (int i=0; i<r; i++)
	{
		total[i]=0;
		for (int j=2; j<c; j++)
		{
			total[i] += marks[i][j];
		}
	}
	
	//plus team marks
	for (int i=0; i<4; i++)
	{
		totalteam[0] += total[i];
	}
	
	for (int i=4; i<8; i++)
	{
		totalteam[1] += total[i];
	}

	for (int i=8; i<r; i++)
	{
		totalteam[2] += total[i];
	}
	
	//find winner
	indwinner= findIndWinner(total, a, b);
	teamwinner= findTeamWinner(totalteam, d);
	
	//output
	displayLine();
	cout << endl;
	cout << "Id\tE1\tE2\tE3\tE4\tE5\tTOTAL" << endl;
	displayLine();
	cout << endl;
	
	//team1
	cout << "TEAM " << marks[0][0] << endl;
	for (int i=0; i<4; i++)
	{
		for (int j=1; j<c; j++)
		{
			cout << marks[i][j] << "\t";
		}
		cout << total[i] << endl;
	}
	cout << "TOTAL" << setw(46) << totalteam[0] << endl;
	displayLine();
	cout << endl;
	
	//team2
	cout << "TEAM " << marks[4][0] << endl;
	for (int i=4; i<8; i++)
	{
		for (int j=1; j<c; j++)
		{
			cout << marks[i][j] << "\t";
		}
		cout << total[i] << endl;
	}
	cout << "TOTAL" << setw(46) << totalteam[1] << endl;
	displayLine();
	cout << endl;
	
	//team3
	cout << "TEAM " << marks[8][0] << endl;
	for (int i=8; i<r; i++)
	{
		for (int j=1; j<c; j++)
		{
			cout << marks[i][j] << "\t";
		}
		cout << total[i] << endl;
	}
	cout << "TOTAL" << setw(46) << totalteam[2] << endl;
	displayLine();
	cout << endl;
	
	cout << "Winner for Individual Category: " << marks[a][1] << " (Team " << b << ")" << endl;	
	cout << "Winner for Group Category: Team " << d << " (Score = " << teamwinner << ")" << endl << endl;
	cout << "Press any key to continue..." << endl;
		
	inp.close();
	return 0;
}
