#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#define C 3
#define R 20
using namespace std;


void getInput(string (&nameUni)[R][1], int (&number)[R][C])
{
	
	ifstream inp;
	inp.open("input1.txt");
	ofstream out("output.txt");
	
	if (!inp.is_open())
	{
		cout << "ERROR!! ";
		exit(0);
	}
	
	for(int i = 0; i < R; i++) //sepatutnya yang ni akan print data kat ouput file tapiiii dia tak keluar anddd, (tengok kat main)
	{
		while(inp >> nameUni[i][0] >> number[i][0] >> number[i][1] >> number[i][2] )
		{
       		out << nameUni[i][0] << " " << number[i][0] << " " << number[i][1]
    		<< " " << number[i][2] << endl;
    	}
	}
	out.close();
	inp.close();
}

int calTotal(int number[][C], int total[])
{
	for(int i = 0; i < C; i++)
	{
		total[i] = 0;
		for (int j = 0; j < R; j++)
		{
			total[i] += number[i][j];
		}
	}	
		
	
}

int getLowest(int number[R][C], int &lowestI, int &lowestE, int &lowestO, int &a, int &b, int &c) //Lowest value in the array
{
	lowestI = lowestE = lowestO = 9999;
	for (int i = 0; i < R; i++) 
	{
		if (number[i][0] < lowestI)
		{
			lowestI = number[i][0];
			a = i + 1;
		}
		
		if (number[i][1] < lowestE)
		{
			lowestE = number[i][1];
			b = i + 1;
		}
		
		if (number[i][2] < lowestO)
		{
			lowestO = number[i][2];
			c = i + 1;
		}
		
	}
}

int getHighest(int number[R][C]) //Highest value in the array
{
	int highest = number[0][0];
	for (int i = 0; i < R; i++) 
	{
		for (int j = 0; j < C; j++) 
		{
			if (number[i][j] > highest)
				highest = number[i][j];
		}
	}
	return highest;
}

int main()
{
	int number[R][C], total[3], lowestI, lowestE, lowestO, a, b, c;
	int highestIntake, highestEnrolment, highestOutput;
	string nameUni[R][1];
	ifstream inp("input1.txt");
	ofstream out("output.txt");
	getInput(nameUni, number);

	
	out << "\tNUMBER OF STUDENTS' INTAKE, ENROLMENT AND OUTPUT\n\t\tIN PUBLIC UNIVERSITIES (2015) " << endl;
	out << "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ";
	out << "\n\tUNIVERSITY\t\t\tINTAKE\t\tENROLMENT\t\tOUTPUT";
	out << "\n_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ " <<  endl;
	
	for(int i = 0; i < R; i++) // bila copy yang tadi pastu paste kat sini dia keluar kat output file!!!!!
	{
		while(inp >> nameUni[i][0] >> number[i][0] >> number[i][1] >> number[i][2] )
		{
       		out << nameUni[i][0] << " " << number[i][0] << " " << number[i][1]
    		<< " " << number[i][2] << endl;
    	}
	}
	
	calTotal(number, total);
	getLowest(number, lowestI, lowestE, lowestO, a, b, c);
	
	highestIntake = getHighest(number);
	highestEnrolment = getHighest(number);
	highestOutput = getHighest(number);
	
	out << "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ " << endl;
	out << "TOTAL\t\t\t\t\t" << total[0] << "\t\t" << total[1] << "\t\t" << total[2] << endl;
	out << "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \n";
	out << "Lowest : " << lowestI << endl;
	out << "Lowest Enrolment : " << lowestE << endl;
	out << "Lowest Output : " << lowestO << endl;
	out << "Highest Intake : " << highestIntake << endl;
	out << "Highest Enrolment : " << highestEnrolment << endl;
	out << "Highest Output : " << highestOutput << endl;
	out << "Range Intake : " << highestIntake - lowestI<< endl;
	out << "Range Enrolment : " << highestEnrolment - lowestE << endl;
	out << "Range Output : " << highestOutput - lowestO << endl;
	
	inp.close();
	out.close();
		
	return 0;
}
