/*
NUR ALEEYA SYAKILA BINTI MUHAMAD SUBIAN A19EC0127 000726100810
NUR HADIRAH MUNAWARAH BINTI ROZMIZAN A19EC0201 000526060076
*/

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define r 20
#define c 4

int getInput(char u[r][7], int inta [r], int enro [r], int outp [r])
{
	ifstream inp;
	inp.open("input1.txt");
   
	
	if(!inp) 
	{
		cout<<"Error!!"<<endl;
		exit(0);
	}
	for (int i=0;i<r;i++)
	{
		inp>>u[i]>>inta[i]>>enro[i]>>outp[i];
	}
	inp.close();
	
}
int calTotal( int t[])
{
	int total=0;
	
	for (int j=0;j<r;j++ )
	{
	 total+=t[j];
	}
	return total;
}

int getLowest (char u[][7], int l[], char lowestuni [7])
{
	int lowest=99999999;
	strcpy(lowestuni,u[0]);
	for (int i=0;i<r;i++)
	{
		if(l[i]<lowest)
		{
			lowest=l[i];
			strcpy (lowestuni,u[i]);
		}
	}
	return lowest;
	
}
int getHighest (char u[][7] ,int h[], char highuni [7])
{
	int highest=-99999999;
	strcpy(highuni,u[0]);
	for (int i=0;i<r;i++)
	{
		if(h[i]>highest)
		{
			highest=h[i];
			strcpy (highuni,u[i]);
		}
	}
	
	return highest;
	
}
float average(int a)
{
	float ave=static_cast<float>(a)/r;
	return ave;
	
}



int main()
{
	
	ofstream out("output.txt");
	
	out<<"	NUMBER OF STUDENTS' INTAKE, ENROLMENT AND OUTPUT		"<<endl;
	out<<" 		IN PUBLIC UNIVERSITIES (2015)		"<<endl;
	out<<"---------------------------------------------------------------"<<endl;
	out<<setw(10)<<" UNIVERSITY"<<setw(17)<<"INTAKE"<<setw(14)<<"ENROLMENT"<<setw(11)<<"OUTPUT"<<endl;
	out<<"---------------------------------------------------------------"<<endl;
	
	char uni[r][7];
	char lowuni1[7],lowuni2[7],lowuni3[7],hiuni1[7],hiuni2[7],hiuni3[7];
	
	int intake [r], enr[r], output[r];
	getInput(uni,intake,enr,output);
	
	
	
	for (int i=0;i<r;i++)
	{
	out<<left <<"\t"<< setw(6)<<uni[i];
	out<<right<<setw(18)<<intake[i]<<setw(11)<<enr[i]<<setw(13)<<output[i]<<endl;
	}
	
	int totint= calTotal(intake);
	int totenr= calTotal(enr);
	int totout=calTotal(output);
	
	int lowint= getLowest(uni,intake,lowuni1);
	int lowenr= getLowest(uni,enr,lowuni2);
	int lowout=getLowest (uni,output,lowuni3);
	
	int hiint= getHighest(uni,intake,hiuni1);
	int hienr= getHighest(uni,enr,hiuni2);
	int hiout=getHighest (uni,output,hiuni3);
	
	float avein=average(totint);
	float aveenr=average(totenr);
	float aveout=average(totout);
	
	out<<"---------------------------------------------------------------"<<endl;
	out << setw(7)<<left<<"\tTOTAL "<<right<<setw(18)<< totint << setw(11)<< totenr << setw(13) <<totout <<endl;
	out <<fixed <<setprecision(2);
	out << setw(7) <<left<< "\tAVERAGE"<<right<<setw(17)<<avein <<setw(11)<<aveenr<<setw(13)<< aveout<<endl;
	out<<"---------------------------------------------------------------"<<endl<<endl;
	out <<"THE LOWEST NUMBER OF STUDENTS' INTAKE    = " << lowint << "("<<lowuni1<<")"<<endl;
	out << "THE LOWEST NUMBER OF STUDENTS' ENROLMENT = "<< lowenr << "("<<lowuni2<<")" <<endl;
	out << "THE LOWEST NUMBER OF STUDENTS' OUTPUT    = " << lowout << "("<<lowuni3<<")" <<endl <<endl;
	out <<"THE HIGHEST NUMBER OF STUDENTS' INTAKE    = "<< hiint << "("<<hiuni1<<")" <<endl;
	out <<"THE HIGHEST NUMBER OF STUDENTS' ENROLMENT = "<< hienr << "("<<hiuni2<<")" <<endl;
	out <<"THE HIGHEST NUMBER OF STUDENTS' OUTPUT    = " << hiout << "("<<hiuni3<<")" << endl<<endl;
	out  <<"THE RANGE OF NUMBER OF STUDENTS' INTAKE    = " << hiint-lowint <<endl;
	out  <<"THE RANGE OF NUMBER OF STUDENTS' ENROLMENT = "<<hienr-lowenr<<endl;
	out  <<"THE RANGE OF NUMBER OF STUDENTS' OUTPUT   = " <<hiout-lowout<<endl <<endl;
	out<<"---------------------------------------------------------------"<<endl;



	out.close();
	return 0;
}
