//----------------------------------------------------------------------------- // Find the min and max of a list of students. // Data includes last name, first name, ID, and grade. For example: // // duck donald 5000 80 // mouse mickey 2200 75 // witch wicked 7800 92 // // Assumptions: // -- Format of data is correct. // -- All names are no more than MAXLENGTH. // -- IDs are correctly sized as integers. //----------------------------------------------------------------------------- #include #include #include using namespace std; int const MAXSIZE = 50; // maximum number of students int const MAXLENGTH = 30; // maximum string length struct Student { // information on one student int id; int grade; char lastName[MAXLENGTH]; char firstName[MAXLENGTH]; // more data as needed }; // prototypes bool readData(Student [], int& ); // read data for class of students void printList(const Student [], int); // print student list bool lessThan(int, int); // is first < second? bool greaterThan(int, int); // is first > second? int findExtreme(const Student [], int, bool (*)(int,int)); //---------------------------------- main ----------------------------------- int main() { Student myClass[MAXSIZE]; // all the students int size = 0; // total number of students // get student data from a file, fill array then sort and print bool success = readData(myClass, size); if (success) { printList(myClass, size); // print student list cout << endl; // find high and low grade int high = findExtreme(myClass, size, greaterThan); int low = findExtreme(myClass, size, lessThan); cout << "The high grade is " << high << endl; cout << "The low grade is " << low << endl; } return 0; } //----------------------------------------------------------------------------- // readData // read the data from a file for one class of students, place into array //----------------------------------------------------------------------------- bool readData(Student myClass[], int& size) { // create file object and open the datafile ifstream infile("ptrToFunc.txt"); if (!infile) { cerr << "File could not be opened." << endl; return false; } while (size < MAXSIZE && infile >> myClass[size].lastName >> myClass[size].firstName >> myClass[size].id >> myClass[size].grade ) size++; return true; } //----------------------------------------------------------------------------- // printList // print the list of students //----------------------------------------------------------------------------- void printList(const Student myClass[], int size) { for (int i=0; i < size; i++) cout << setw(4) << myClass[i].id << setw(4) << myClass[i].grade << " " << myClass[i].lastName << " " << myClass[i].firstName << endl; } //----------------------------------------------------------------------------- // lessThan // return true if parameter one is less than parameter two, false othewise //----------------------------------------------------------------------------- bool lessThan(int num1, int num2) { return num1 < num2; } //----------------------------------------------------------------------------- // greaterThan // return true if parameter one is greater than parameter two, false othewise //----------------------------------------------------------------------------- bool greaterThan(int num1, int num2) { return num1 > num2; } //----------------------------------------------------------------------------- // findExtreme // find the extreme value, determined by compare function, in an array of ints //----------------------------------------------------------------------------- int findExtreme(const Student myClass[], int size, bool (*compare)(int,int)) { int extreme = myClass[0].grade; for (int i=0; i < size; i++) { if ((*compare)(myClass[i].grade, extreme)) extreme = myClass[i].grade; } return extreme; }