#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#define R 20
#define C 3
using namespace std;

void getInput (int input[][C], string uni[R][1]);
void calcTotal(int input[][C], int&,int&,int&);
void getLowest(int input[][C] ,int& , int& , int&);
void getHighest(int input[][C], int & , int & , int &);
void displayLine();
ofstream out("output.txt");
int main(){
    int input[R][C];
 
    int tot_i = 0 , tot_e = 0 , tot_o = 0;
    int low_i = 0 , low_e = 0 , low_o = 0;
	int hihi = 0, hihe = 0, hiho = 0;
	float avgi, avge , avgo;
	 
    string uni[R][1];
    getInput(input, uni);
    calcTotal(input, tot_i , tot_e, tot_o); 
    getLowest(input, low_i , low_e , low_o );
    getHighest(input, hihi , hihe, hiho);
    
    avgi = static_cast <float>(tot_i)/20;
	avge = static_cast<float>(tot_e)/20;
	avgo = static_cast<float>(tot_o)/20;
	
    out << setw(80) << "NUMBER OF STUDENTS' INTAKE, ENROLMENT AND OUTPUT " << endl;
    out << setw(70) << "IN PUBLIC UNIVERISITIES (2015) " << endl << endl;
    displayLine();
    out << setw(15) << "UNIVERISITY" << setw(20) << "INTAKE" << setw(25) << "ENROLMENT" << setw(25) << "OUTPUT" << endl;
    displayLine();
    
    for(int i = 0 ; i < 20 ; i++ ){
    	out << setw(10) << uni[i][0];
    	for(int j = 0 ; j < 3 ; j++){
    		out << setw(25) << input[i][j];
		}
		out << endl;
	}
	
	out << endl;
	displayLine();
	out << fixed << setprecision(2);
    out << setw(10) << " TOTAL" << setw(25) << tot_i << setw(25) << tot_e <<setw(25) << tot_o << endl;
	out << setw(10) << "AVERAGE" << setw(25) << avgi << setw(25) << avge << setw(25) << avgo << endl;
    displayLine();
	out << endl << endl;
	out << "THE LOWEST NUMBER OF STUDENTS' INTAKE = " << input[low_i][0] << "(" << uni[low_i][0] << ")" << endl;
    out << "THE LOWEST NUMBER OF STUDENTS' ENROLMENT = " << input[low_e][1] << "(" << uni[low_e][0] << ")" << endl;
    out << "THE LOWEST NUMBER OF STUDENTS' OUTPUT = " << input[low_o][2] << "(" << uni[low_o][0] << ")" << endl << endl << endl;
    out << "THE HIGHEST NUMBER OF STUDENTS' INTAKE = " << input[hihi][0] << "(" << uni[hihi][0] << ")" << endl;
    out << "THE HIGHEST NUMBER OF STUDENTS' ENROLMENT = " << input[hiho][1] << "(" << uni[hiho][0] << ")" << endl;
    out << "THE HIGHEST NUMBER OF STUDENTS' OUTPUT = " << input[hihe][2] << "(" << uni[hihe][0] << ")" << endl << endl << endl;
    out << "THE RANGE OF NUMBER OS STUDENTS' INTAKE = " << input[hihi][0] - input[low_i][0] << endl;
	out << "THE RANGE OF NUMBER OS STUDENTS' ENROLMENT = " << input[hihe][1] - input[low_e][1] << endl;
	out << "THE RANGE OF NUMBER OS STUDENTS' OUTPUT = " << input[hiho][2] - input[low_o][2] << endl << endl;
	displayLine();
	out.close();
	return 0;
}

void getInput(int input[][C], string uni[][1] ){
	ifstream inp;
	string n1;
	int i=0;
	inp.open("input1.txt");
	if(!inp){
		cout << "Error!";
		exit(0);
	}
	while(inp >> uni[i][0]){
	inp >> input[i][0];
	inp >> input[i][1];
	inp >> input[i][2];
	i++;	
	}
    inp.close();
}

void calcTotal(int input[][C], int &tot_i, int &tot_e , int &tot_out){
	for(int j = 0; j < 20 ; j++) {
		tot_i += input[j][0];
		tot_e += input[j][1];
		tot_out += input[j][2];
		}
}

void getLowest(int input[][C], int &li , int &le , int &lo){
	int low_i = input[0][0] , low_e = input[0][1] , low_o = input[0][2];
	
	for(int j = 0 ; j < 20 ; j++) {
		if(input[j][0] < low_i){
			low_i = input[j][0];
			li = j;
		} 
		if(input[j][1] < low_e){
			low_e = input[j][1];
			le = j; 
		} 
		if(input[j][2] < low_o){
			low_o = input[j][2];
			lo = j;
		}
	}
}

void getHighest(int input[][C], int &hi , int &he , int &ho){
	int high_i = input[0][0] , high_e = input[0][1] , high_o = input[0][2];
	
	for(int j = 0 ; j < 20 ; j++) {
		if(input[j][0] >  high_i){
			high_i = input[j][0];
			hi = j;
		} 
		if(input[j][1] > high_e){
			high_e = input[j][1];
			he = j; 
		} 
		if(input[j][2] > high_o){
			high_o = input[j][2];
			ho = j;
		}
	}
}


void displayLine(){
for(int j = 0 ; j < 100 ; j++)
   out << "-";  
   out << endl;
}
