#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int buildAr(int a[], float b[], float c[],float d[]);
void displayAr( int a[], float b[], float c[],float d[], int e);
void sortAr(int a[], float b[], float c[], int e);
void searchAr(int a[], float b[], float c[], float d[], int e);
int main()
{
int ARL=18;
int studentID[ARL];
float progAvg[ARL];
float testAvg[ARL];
float overallAvg[ARL];
int num, i, studentcount;
ifstream inFile;
studentcount = buildAr(studentID,progAvg,testAvg,overallAvg);
displayAr(studentID,progAvg,testAvg,overallAvg,ARL);
sortAr(studentID,progAvg,testAvg,studentcount);
system("pause");
system("cls");
cout<<"\n\nSORTED LIST: \n\n";
displayAr(studentID,progAvg,testAvg,overallAvg,ARL);
system("pause");
searchAr(studentID,progAvg,testAvg,overallAvg,ARL);
system("pause");
return 0;
}
/**********************************************************************************
Function: buildAr
Use: Builds the three arrays from the numbers in the text file.
Arguments: 1. studentID[]: the array that contains the student ID numbers.
2. progAvg[]: the array that contains the program grade averages.
3. testAvg[]: the array that contains the test grade averages.
4. overallAvg[]: the array that contains the overall grade averages.
Returns: nothing
**********************************************************************************/
int buildAr(int studentID[], float progAvg[], float testAvg[], float overallAvg[])
{
ifstream inFile;
inFile.open("averages.txt" );
if ( inFile.fail() )
{
cout << "input file did not open";
exit(0);
}
int i=0;
while(!inFile.eof())
{
inFile >> studentID[i];
inFile >> progAvg[i];
inFile >> testAvg[i];
overallAvg[i]=(progAvg[i]*.4)+(testAvg[i]*.6);
i++;
}
inFile.close();
//return studentID;
//return progAvg;
//return testAvg;
}
/**********************************************************************************
Function: displayAr
Use: Displays all of the arrays in a nice format.
Arguments: 1. studentID[]: the array that contains the student ID numbers.
2. progAvg[]: the array that contains the program grade averages.
3. testAvg[]: the array that contains the test grade averages.
4. overallAvg[]: the array that contains the overall grade averages.
5. ARL: the constant that contains the number of elements in the array.
Returns: nothing
**********************************************************************************/
void displayAr( int studentID[], float progAvg[], float testAvg[], float overallAvg[], int ARL)
{
cout<<"STUDENT ID Progam Average Test average Over All\n";
cout<<"------------------------------------------------------------------\n";
for (int i = 0; i < ARL; i++)
{
cout<<studentID[i]<<setw(20)<<progAvg[i]<<setw(20)<<testAvg[i]
<<setw(20)<<overallAvg[i]<<endl;
}
}
/**********************************************************************************
Function: sortAr
Use: Sorts out the elements in the arrays and puts them in order.
Arguments: 1. studentID[]: the array that contains the student ID numbers.
2. progAvg[]: the array that contains the program grade averages.
3. testAvg[]: the array that contains the test grade averages.
4. studentcount: the constant that contains the number of elements in the array.
Returns: nothing
**********************************************************************************/
void sortAr( int studentID[], float progAvg[], float testAvg[], int studentcount)
{
int top = 0, last = 19, ptr, ssf;
int temp;
float temp2;
while ( top < last)
{
ptr = top;
ssf = top;
while ( ptr <= last)
{
if ( studentID[ptr] < studentID[ssf])
{
ssf = ptr;
}
else
{
}
ptr++;
}
temp = studentID[top];
studentID[top] = studentID[ssf];
studentID[ssf] = temp;
temp2 = progAvg[top];
progAvg[top] = progAvg[ssf];
progAvg[ssf] = temp;
temp2 = testAvg[top];
testAvg[top] = testAvg[ssf];
testAvg[ssf] = temp;
top++;
}
}
/*{
int x=0;
int y=0;
int last=19;
int sstudentID[studentcount];
float sprogAvg[studentcount];
float stestAvg[studentcount];
float soverallAvg[studentcount];
for (int i = 0; i < ARL; i++)
{
sstudentID[i]=studentID[i];
}
for (int i = 0; i < ARL; i++)
{
while(y !=studentcount)
{
{
if(sstudentID[i]>last)
{
last=sstudentID[i];
cout<<"HERE!";
}
y++;
}
}
studentID[i]=studentID[y];
progAvg[i]=progAvg[y];
testAvg[i]=testAvg[y];
overallAvg[i]=overallAvg[y];
sstudentID[y]=0;
}
}
*/
/**********************************************************************************
Function: searchAr
Use: Allows the user to search for student ID numbers in the arrays.
Arguments: 1. studentID[]: the array that contains the student ID numbers.
2. progAvg[]: the array that contains the program grade averages.
3. testAvg[]: the array that contains the test grade averages.
4. overallAvg[]: the array that contains the overall grade averages.
5. ARL: the constant that contains the number of elements in the array.
Returns: nothing
**********************************************************************************/
void searchAr( int studentID[], float progAvg[], float testAvg[],float overallAvg[], int ARL)
{
system("cls");
int search=0;
int z=0;
cout<<"Please enter the students id: ";
cin>> search;
while(z != ARL)
{
if(studentID[z]==search)
{
cout<<"\n\n\nStudent ID: "<<studentID[z]<<" has a program average of: "<<progAvg[z]<<" a test average of "<<testAvg[z]<<" and an overall average of "<<overallAvg[z]<<endl;
system("pause");
main();
}
z++;
}
cout<<"\n\n\nStudent ID: "<<search<<" not found.\n\n";
system("pause");
system("cls");
main();
}