Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* 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 #include #include #include using namespace std;

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 studentRecords, int lower, int higher) { // pivot being set int pivot = studentRecords[higher];

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(vector studentRecords, int lower, int higher) { if (lower < higher) { int partInd = partition(studentRecords, lower, higher);

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 studentRecords) { int size = studentRecords.size(); for (int i = 0; i < size; i++) { cout << "Student ID: " << studentRecords.at(i).GetStudentID() << " "; cout << "First Name: " << studentRecords.at(i).GetStudentFirst() << " "; cout << "Middle Name: " << studentRecords.at(i).GetStudentMiddle() << " "; cout << "Last Name: " << studentRecords.at(i).GetStudentLast() << endl; } } };

/* 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 studentVector;

// 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions