Question
/* Implementation of the main() method of the program. */ #include #include #include #include using namespace std; class Student { public: // default constructor Student()
/* Implementation of the main() method of the program. */
#include
class Student { public: // default constructor Student() { studentID = 0; studentFirst = "First"; studentMiddle = "Middle"; studentLast = "Last"; } void SetStudentID(int number) { studentID = number; } void SetStudentFirst(string name) { studentFirst = name; } void SetStudentMiddle(string name) { studentMiddle = name; } void SetStudentLast(string name) { studentLast = name; } int GetStudentID() { return studentID; } string GetStudentFirst() { return studentFirst; } string GetStudentMiddle() { return studentMiddle; } string GetStudentLast() { return studentLast; }
private: int studentID; string studentFirst; string studentMiddle; string studentLast; };
// overloading the greater than operator bool operator < (Student first, Student second) { // comparing based on first name of the student return first.GetStudentID() < second.GetStudentID(); }
/* Class StudentQuickSorter to sort and print the Students vector */ class StudentQuickSorter : public Student { public:
// TODO: // This function selects the last element in the array as the pivot, puts // the pivot in the right place and places all lesser elements to the left // of the pivot element and all greater elements to the right. double partition(vector
int i = (lower - 1); // Index for smaller element
for (j = lower; j <= higher - 1; j++) { // Check if element is <= pivot. if (studentRecords[j] <= pivot) { i++; // increment index of smaller element Swap(&studentRecords[i], &studentRecords[j]); } } Swap(&studentRecords[i + 1], &studentRecords[higher]); return (i + 1); }
void Swap(Student *first, Student *second) { Student temp = *first; *first = *second; *second = temp; }
// This method will receive an array of Student records to be sorted. // It will sort the records by Student ID(s). It will also return the // sorted array of Student records. vector
Sort(studentRecords, lower, partInd - 1); // Before partition Sort(studentRecords, partInd + 1, higher); // After partition } }
// This method will receive an array of Student records. It will print // out the information of each Student record. void PrintRecords(vector
/* main function for program execution */ int main() { int studentRecordSize;
// Prompt the user for the number of Student records to be entered. cout << "Enter the number of student records to be input: "; cin >> studentRecordSize; vector
// Prompt the user to enter the information of each Student records // up to the number of records specified above. for (int i = 0; i < studentRecordSize; i++) { Student newStudent; string fn, mn, ln; int id; cout << "STUDENT #" << (i + 1) << endl;
cout << "Enter Student's ID: "; cin >> id; newStudent.SetStudentID(id);
cout << "Enter Student's first name: "; getline(cin >> ws, fn); newStudent.SetStudentFirst(fn);
cout << "Enter Student's middle name: "; getline(cin >> ws, mn); newStudent.SetStudentMiddle(mn);
cout << "Enter Student's last name: "; getline(cin >> ws, ln); newStudent.SetStudentLast(ln);
cout << endl; studentVector.push_back(newStudent); }
// TODO: // Determines min/max for sorting function int minEle = min_element(studentVector.begin(), studentVector.end(), [](Student &a, Student &b) {return a.GetStudentID() > b.GetStudentID(); }); int maxEle = max_element(studentVector.begin(), studentVector.end(), [](Student &a, Student &b) {return a.GetStudentID() < b.GetStudentID(); });
StudentQuickSorter sorter; // Print the original (unsorted) array. cout << "Original Student Records." << endl; sorter.PrintRecords(studentVector);
// Sorting the Student vector. studentVector = sorter.Sort(studentVector);
// Print the sorted array. cout << " Sorted Student Records" << endl; sorter.PrintRecords(studentVector);
system("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started