
//AINA ALIAH BINTI RUSLAN (A19EC0012)
//SHAKIRAH BINTI MOHD SHUKUR (A19EC0159)


#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#define R 20
#define C 3
using namespace std ;


void getInput (string name [][1], int student[][C])  //column wajib ada
{
	ifstream inp  ;
	ofstream out ;
	
	inp.open ("input1.txt") ;
	if (! inp)  
	{
		out << "Error !!!" << endl ;
		exit(0) ;  
	}
	
	int i = 0 ;	
	while ( inp >> name[i][0] )
	{
	    inp >> student[i][0] ;
	    inp >> student[i][1] ;
	    inp >> student[i][2] ;

	    i++ ;	
	}
	inp.close() ;
}

int calTotal (int student[][C], int &sum1, int &sum2, int &sum3)
{
  	 for (int i = 0 ; i < R  ; i++)		     
	    {	
				    sum1+=student[i][0];
			    	sum2 += student[i][1];
			    	sum3 +=  student [i][2] ;
		   
	    }	
}

int getLowest(int student [][C], int &lowin, int &lowen, int &lowout)
{
	int low1 = student[0][0] ;
	int low2 = student[0][1] ;
	int low3 = student[0][2] ;
		
	for (int i = 0 ; i < R ; i++)
	{
		for (int j = 0 ; j < C ; j++)
		{
			if ( student[i][0] < low1 )
			{
				low1 = student [i][j] ;
			 	lowin = i;   //index untuk display kat main	
			}
			
			if ( student[i][1] < low2 )
			{
			 	low2 = student [i][j] ;
			 	lowen = i;   //index untuk display kat main
			}
			 	
			if ( student[i][2] < low3 )
			{
			 	low3 = student [i][2] ;
			 	lowout = i;   //index untuk display kat main
			}
		}
	}
}

int getHighest(int student [][C], int &highin, int &highen, int &highout)
{
	int high1 = student[0][0] ;
	int high2 = student[0][1] ;
	int high3 = student[0][2] ;
	
	for (int i = 0; i < R; i++)
	{
		for ( int j = 0 ; j < C ; j++)
		{
			if ( student[i][0] > high1 )
			{
				high1 = student [i][j] ;
			 	highin = i;   //index untuk display kat main
			}

			if ( student[i][1] > high2 )
			{
				high2 = student [i][j] ;
			 	highen = i;   //index untuk display kat main
			}

			if ( student[i][2] > high3 )
			{
				high3 = student [i][j] ;
			 	highout = i;   //index untuk display kat main
			}			
		}
	}
}

int main ()
{
	ifstream inp ("ass2q1.txt") ;
	
	string name[R][1] ;    //nak baca nama U column 1 so row yang berubah column const
	int student[R][C] ;    //nak baca no student column 0,1,2 dan semua row
	int sum1 = 0, sum2 = 0, sum3 = 0 ;
	float avg1, avg2, avg3 ;     // [1] INTAKE   [2] ENROLMENT   [3] OUTPUT
	int low1 = 0 , low2 = 0 , low3 = 0 ;      //no  student tak boleh negatif
	int high1 = 0 , high2 = 0 , high3 = 0 ;   //no student tak boleh negatif
		
		getInput (name, student) ;
		calTotal (student, sum1, sum2, sum3) ;
		
		avg1 = sum1 / 20.0 ;
		avg2 = sum2 / 20.0 ;
		avg3 = sum3 / 20.0 ;
    
        getLowest(student, low1, low2, low3) ;
        getHighest(student, high1, high2, high3) ;

        
    ofstream out ("output.txt") ;
    out << left;
	out << endl ;
		
	out << "   NUMBER OF STUDENTS' INTAKE, ENROLMENT AND OUTPUT " << endl ;
	out << "\t\t IN PUBLIC UNIVERSITIES (2015) " << endl << endl ;
	out << "---------------------------------------------------------------" << endl ;
	out << setw(15) <<"UNIVERSITY" << setw(15) << "INTAKE" << setw(15) << "ENROLMENT" << setw(15) << "OUTPUT " << endl ;
	out << "---------------------------------------------------------------" << endl ;
    
	//getinput    
    for (int i = 0 ; i < R ; i++)
    {
    	out << setw(15) << name[i][0]   
			<< setw(15) << student[i][0] 
			<< setw(15) << student[i][1] 
			<< setw(15) << student[i][2] << endl ;
	}
	
	out << "---------------------------------------------------------------" << endl ;
	
	//total
    out  << setw(15) << "TOTAL" << setw(15) << sum1 << setw(15) << sum2 << setw(15)  << sum3 << endl ;
    
    //average
    out << fixed << setprecision(2) ;
    out << setw(15) << "AVERAGE " << setw(15) << avg1 << setw(15) << avg2 << setw(15)  << avg3 << endl ;
    
    out << "---------------------------------------------------------------" << endl << endl ;
           
    //lowest
	out << "THE LOWEST NUMBER OF STUDENTS' INTAKE = " << student[low1][0] ;	
	out << " (" << name[low1][0] << ")" << endl ;

	out << "THE LOWEST NUMBER OF STUDENTS' ENROLMENT = " << student[low2][1] ;
	out << " (" << name[low2][0] << ")" << endl ;
    
	out << "THE LOWEST NUMBER OF STUDENTS' OUTPUT = " << student[low3][2] ;
	out << " (" << name[low3][0] << ")" << endl << endl ;
        
    //highest
	out << "THE HIGHEST NUMBER OF STUDENTS' INTAKE = " << student[high1][0] ;	
	out << " (" << name[high1][0] << ")" << endl ;

	out << "THE HIGHEST NUMBER OF STUDENTS' ENROLMENT = " << student[high2][1] ;
	out << " (" << name[high2][0] << ")" << endl ;
    
	out << "THE HIGHEST NUMBER OF STUDENTS' OUTPUT = " << student[high3][2] ;
	out << " (" << name[high3][0] << ")" << endl << endl ;    
        
   //range     
    out << "THE RANGE OF NUMBER OF STUDENTS' INTAKE = " << student[high1][0] - student[low1][0] << endl ;
    out << "THE RANGE OF NUMBER OF STUDENTS' ENROLMENT = " << student[high2][1] - student[low2][1] << endl ;
    out << "THE RANGE OF NUMBER OF STUDENTS' OUTPUT = " << student[high3][2] - student[low3][2] << endl << endl ;
    
    out << "---------------------------------------------------------------" << endl << endl ;
        
	inp.close () ;  //tutup file
	out.close () ;
 	return 0 ;
 	
}
