#include <iostream>
#include <iomanip>
#include <fstream>
const int ARL=19;
using namespace std;
struct student
{
int studentID;
double programAvg;
double testAvg;
};
int buildAr(student a[]);
void displayAr( student a[], int b);
void sortAr(student a[], int b);
void searchAr(student a[], int b, int c);
int main()
{
//initializing a bunch of arrays and such
int num, i, studentcount;
int search1 = 978179;
int search2 = 159753;
student student[ARL];
ifstream inFile;
studentcount = buildAr(student); //calling functions in order needed
displayAr(student, ARL);
sortAr(student, ARL);
system("pause");
system("cls");
cout<<"\n\nSORTED LIST: \n\n";
displayAr(student, ARL);
system("pause");
searchAr(student, search1, search2);
system("pause");
return 0;
}
/**********************************************************************************
Function: buildAr
Use: Builds the three arrays from the numbers in the text file.
Arguments: 1. student[]: the structure that contains the student ID numbers.
Returns: nothing
**********************************************************************************/
int buildAr(student student[])
{
ifstream inFile;
inFile.open("averages.txt" );
if ( inFile.fail() )
{
cout << "input file did not open";
exit(0);
}
/* int num;
char str[80];
inFile.getline(str,80);
sscanf(str, "%d", &num)
*/
int i=0;
while(!inFile.eof())
{
inFile >> student[i].studentID;
inFile >> student[i].programAvg;
inFile >> student[i].testAvg;
i++;
}
inFile.close();
//return studentID;
//return progAvg;
//return testAvg;
}
/**********************************************************************************
Function: displayAr
Use: Displays all of the arrays in a nice format.
Arguments: 1. student[]: the structure that contains the student ID numbers.
2. ARL: the constant that contains the number of elements in the array.
Returns: nothing
**********************************************************************************/
void displayAr(student student[],int ARL)
{
//overallAvg[i]=(student[i].programAvg*.4)+(student[i].testAvg*.6);
cout<<"STUDENT ID Progam Average Test average Over All\n";
cout<<"------------------------------------------------------------------\n";
for (int i = 0; i < ARL; i++)
{
cout<<student[i].studentID<<setw(20)<<student[i].programAvg<<setw(20)
<<student[i].testAvg<<endl//setw(20)<<overallAvg[i]<<endl
;
}
}
/**********************************************************************************
Function: sortAr
Use: Sorts out the elements in the arrays and puts them in order.
Arguments: 1. student[]: the structure that contains the student ID numbers.
2. ARL: the constant that contains the number of elements in the array.
Returns: nothing
**********************************************************************************/
void sortAr(student student[],int ARL)
{
int top = 0, last = 19, ptr, ssf;
int temp;
float temp2, temp3, temp4;
while ( top < last)
{
ptr = top;
ssf = top;
while ( ptr <= last)
{
if ( student[ptr].studentID < student[ssf].studentID)
{
ssf = ptr;
}
else
{
}
ptr++;
}
student[19] = student[top];
student[top] = student[ssf];
student[ssf] = student[19];
/*
temp2 = student[top].programAvg;
student[top].programAvg = student[ssf].programAvg;
student[ssf].programAvg = temp2;
temp3 = student[top].testAvg;
student[top].testAvg = student[ssf].testAvg;
student[ssf].testAvg = temp3;
temp4 = overallAvg[top];
overallAvg[top] = overallAvg[ssf];
overallAvg[ssf] = temp4;
*/
top++;
}
}
/**********************************************************************************
Function: searchAr
Use: Allows the user to search for student ID numbers in the arrays.
Arguments: 1. student[]: the structure that contains the student ID numbers.
2. search1: The first student ID searched for.
3. search2: The second student ID searched for.
Returns: nothing
**********************************************************************************/
void searchAr(student student[], int search1, int search2)
{
system("cls");
int search=0;
int z=0;
cout<<"Please enter the students id: ";
cin>> search;
while(z != ARL)
{
if(student[z].studentID==search)
{
cout<<"\n\n\nStudent ID: "<<student[z].studentID<<" has a program average of: "
<<student[z].programAvg<<" a test average of "<<student[z].testAvg
//<<" and an overall average of "<<overallAvg[z]<<endl
;
system("pause");
main();
}
if(-1==search)
{
exit(0);
}
z++;
}
cout<<"\n\n\nStudent ID: "<<search<<" not found.\n\n";
system("pause");
system("cls");
main();
}