Question

/* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector>...

/*
Implementation of the main() method of the program.
*/

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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<Student> 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<Student> Sort(vector<Student> 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<Student> 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<Student> 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 << "\nSorted Student Records" << endl;
   sorter.PrintRecords(studentVector);


   system("pause");
   return 0;
}


0 0
Add a comment Improve this question Transcribed image text
Answer #1
Corrected C++ Program
/*
   Implementation of the main() method of the program.
*/

#include <iostream>
#include <string>
#include <vector>
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:
    // student id should be integer
   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.GetStudentFirst() > second.GetStudentFirst();
}

/**
*  Class StudentBubbleSorter to sort and print the Students vector
*/
class StudentBubbleSorter : public Student
{
public:

   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 also return the sorted array of Student records.
   vector<Student> Sort(vector<Student> studentRecords)
   {
       int size = studentRecords.size();
       for (int i = 0; i < size - 1; i++)
           for (int j = 0; j < size - i - 1; j++)
               if (studentRecords[j] > studentRecords[j + 1])
                   Swap(&studentRecords[j], &studentRecords[j + 1]);
       return studentRecords;
   }

   // This method will receive an array of Student records. It will print
   // out the information of each Student record.
   void PrintRecords(vector<Student> 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 entered: ";
   cin >> studentRecordSize;
   vector<Student> 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);
   }

   StudentBubbleSorter 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 << endl << "Sorted Student Records" << endl;
   sorter.PrintRecords(studentVector);
   system("pause");
   return 0;
}
Add a comment
Know the answer?
Add Answer to:
/* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • *HOW DO I CHANGE THIS FROM A VOID FUNCTION TO A NON-VOID WITH PARAMETERS?* #include<iostream> #include<fstream>...

    *HOW DO I CHANGE THIS FROM A VOID FUNCTION TO A NON-VOID WITH PARAMETERS?* #include<iostream> #include<fstream> #include<string> using namespace std; void studentStats() { ifstream inputFile; inputFile.open("outFile.txt"); string studentData; string studentID; string ID, exam1, exam2, exam3; string header; cout << "Enter a Student ID: "; cin >> studentID; bool found =false; while (inputFile) { inputFile >> ID; inputFile >> exam1; inputFile >> exam2; inputFile >> exam3; if (ID.compare(studentID)==0) { cout << ID << " " << exam1 << " " <<...

  • Rework this project to include a class. As explained in class, your project should have its...

    Rework this project to include a class. As explained in class, your project should have its functionalities moved to a class, and then create each course as an object to a class that inherits all the different functionalities of the class. You createclass function should be used as a constructor that takes in the name of the file containing the student list. (This way different objects are created with different class list files.) Here is the code I need you...

  • Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string...

    Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string first, last, job;    double hours, wages, net, gross, tax, taxrate = .40;    double oPay, oHours;    int deductions;    // input section    cout << "Enter First Name: ";    cin >> first;    cout << "Enter Last Name: ";    cin >> last;    cin.ignore();    cout << "Enter Job Title: ";    getline(cin, job);    cout << "Enter Hours Worked:...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT