1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#include<iostream>
#include <fstream>
using namespace std;
struct personType
{
string student;
int age;
double gpa;
char sex;
};
void loadStudents(personType students[], int &numStudents);
void averageAge(personType students[], int );
void over20(personType students[], int );
void highestGpa(personType students[], int );
const int MAXSTUDENTS = 20;
int main()
{
personType students[MAXSTUDENTS];
int numStudents;
//read student information into the array
loadStudents(students, numStudents);
//Print the average age
averageAge(students, numStudents);
//Print the student with the highest gpa
highestGpa(students, numStudents);
//Print the number of students older than 20
over20(students, numStudents);
//print the names of the female students
//femaleStudents(students, numStudents);
//find a student
//findStudent(students, numStudents);
system("pause");
return 0;
}
void loadStudents(personType students[], int &numStudents)
{
cout <<"Entering loadcourses"<<endl;
ifstream infile;
infile.open("Students.dat");
cout <<"Entering while loop"<<endl;
numStudents = 0;
while(!infile.eof() )
infile >> students[numStudents].student >> students[numStudents].age >> students[numStudents].gpa >> students[numStudents].sex;
numStudents++;
}
void averageAge(personType students[], int numStudents)
{
int array_sum;
double average;
cout << "Printing the average age..." << endl;
array_sum = 0;
for (int i = 0; i < numStudents; i++)
array_sum = array_sum + students[i].age;
average = (double)array_sum / numStudents;
cout << array_sum << endl;
cout << average << endl;
}
void highestGpa(personType students[], int numStudents)
{
cout << "Printing the highest GPA..." << endl;
int array_max;
array_max = students[0].gpa;
for (int i = 1; i < numStudents; i++)
if (students[i].gpa > array_max)
{
array_max = students[i].gpa;
}
cout << array_max << endl;
}
void over20(personType students[], int numStudents)
{
cout << "Printing ages over 20" << endl;
int ctr;
ctr = 0;
for (int i = 0; i < numStudents; i++)
if (students[i].age > 20)
{
ctr++;
}
cout << ctr << endl;
}
void femaleStudents(personType students[], int numStudents)
{
}
void findStudent(personType students[], int numStudents)
{
}
|