#include <iostream>
#include <iomanip>
#include <fstream>
#define R 20
using namespace std;

void getInput(char uni[R][10], int intake[R], int enrol[R], int output[R])
{
	ifstream inp("input1.txt");
	ofstream out("output.txt");
	
	if (!inp)
	{
		out << "Error!!" ;
		exit(0);
	}
	
	for (int i=0 ; i<R ; i++)
	{
		inp >> uni[i] >> intake[i] >> enrol[i] >> output[i];
	}
}

int calTotal (int students[])
{
	int total=0;
	
	for (int i=0 ; i<R ; i++)
	{
		total += students[i];
	}
	return total;
}

int getLowest (int students[R], int &lowIn)
{
	int low = students[0];
	
	for (int i=0 ; i<R ; i++)
	{
		if (students[i] < low)
		{
			low = students[i];
			lowIn = i;
		}
	}
	
	return lowIn;
}

int getHighest (int students[R], int &highIn)
{
	int high = students[0];
	
	for (int i=0 ; i<R ; i++)
	{
		if (students[i] > high)
		{
			high = students[i];
			highIn=i;
		}
	}
	
	return highIn;
}

int getRange (int &high, int &low)
{
	return high-low ;
}

int main()
{
	char uni[R][10];
	int intake[R];
	int enrol[R];
	int output[R];
	int highIn=0, lowIn=0;
	
	ifstream inp("input1.txt");
	ofstream out("output.txt");
	
	getInput(uni, intake, enrol, output);
	
	out << "\t\tNUMBER OF STUDENTS' INTAKE, ENROLMENT AND OUTPUT" << endl;
	out << "\t\t\t\tIN PUBLIC UNIVERSITIES (2015)" << endl;
	out << "---------------------------------------------------------------" << endl;
	out << "  UNIVERSITY \t\t INTAKE \t\t ENROLMENT \t\t OUTPUT" << endl;
	out << "---------------------------------------------------------------" << endl;
	
	for(int i=0 ; i<R ; i++)
	{
		out << "\t " << left << setw(15) << uni[i] << right << setw(5) << intake[i] << setw(18) << enrol[i] << setw(16) << output[i] << endl;
	}

	out << "---------------------------------------------------------------" << endl;
	out << "    " << left << setw(15) << "TOTAL" << right << setw(5) << calTotal(intake) << setw(18) << calTotal(enrol) << setw(16) << calTotal(output) << endl;
	out << fixed << setprecision(2);
	out << "    " << left << setw(15) << "AVERAGE" << right << setw(5) << static_cast<float>(calTotal(intake))/R<< setw(18) << static_cast<float>(calTotal(enrol))/R << setw(16) << static_cast<float>(calTotal(output))/R << endl;
	out << "---------------------------------------------------------------" << endl;
	
	int LindexIn = getLowest(intake, lowIn); //index for the lowest intake
	out << endl;
	out << "THE LOWEST NUMBER OF STUDENTS' INTAKE = " << intake[LindexIn] << " (" << uni[LindexIn] << ")" << endl;
	
	int LindexEn = getLowest(enrol, lowIn); //index for the lowest enrolment
	out << "THE LOWEST NUMBER OF STUDENTS' ENROLMENT = " << enrol[LindexEn] << " (" << uni[LindexEn] << ")" << endl;
	
	int LindexOut = getLowest(output, lowIn); //index for the lowest output
	out << "THE LOWEST NUMBER OF STUDENTS' OUTPUT = " << output[LindexOut] << " (" << uni[LindexOut] << ")" << endl;
	
	
	int HindexIn = getHighest(intake, highIn); //index for the highest intake
	out << endl;
	out << "THE HIGHEST NUMBER OF STUDENTS' INTAKE = " << intake[HindexIn] << " (" << uni[HindexIn] << ")"  << endl;
	
	int HindexEn = getHighest(enrol, highIn); //index for the highest enrolment
	out << "THE HIGHEST NUMBER OF STUDENTS' ENROLMENT = " << enrol[HindexEn]<< " (" << uni[HindexEn] << ")" << endl;
	
	int HindexOut = getHighest(output, highIn); //index for the highest output
	out << "THE HIGHEST NUMBER OF STUDENTS' OUTPUT = " << output[HindexOut]<< " (" << uni[HindexOut] << ")" << endl;
	
	out << endl;
	out << "THE RANGE OF NUMBER OF STUDENTS' INTAKE = " << intake[HindexIn] - intake[LindexIn] << endl;
	out << "THE RANGE OF NUMBER OF STUDENTS' ENROLMENT = " << enrol[HindexEn] - enrol[LindexEn] << endl;
	out << "THE RANGE OF NUMBER OF STUDENTS' OUTPUT = " << output[HindexOut] - output[LindexOut] << endl;
	
	out << "---------------------------------------------------------------" << endl;	
}


