Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You're assignment is to write the code for the compare function for this class. This compare function has a small twist. There are two arguments,

You're assignment is to write the code for the compare function for this class. This compare function has a small twist. There are two arguments, the first is an enum tag telling the function which field to use for comparison, the second is the compared Record. As noted in the header file, the function returns an int. The return value of zero indicates that the two objects are the same based on the field requested. Positive return values mean the compared object is lesser (or sequentially before), negative mean the compared object is greater(or sequentially after).

The test code included with this lab will sort a set of Records based on your compare function. The sort is already written (sortBy). When you are running in develop mode enter the field you would like to sort by. If the output does not show that the field are sorted based on the field you requested - there is a bug in your compare function that you will want to fix. You will be tested on all fields, so you should check them all before running in submit mode. You may submit more than once. Edit Record.cpp to get the correct output.

CORRECT OUTPUT:

Unsorted

Name: Bowie, David, years in service:5, grade: 8, last review: Improvement Plan

Name: Kardashian, Kim, years in service:15, grade: 9, last review: Meets Expectations

Name: Kardashian, Khloe, years in service:5, grade: 10, last review: Outstanding

Name: Lee Roth, David, years in service:9, grade: 4, last review: Below Expectations

Name: Garbo, Greta, years in service:1, grade: 3, last review: Outstanding enter a field (0-4) to sort by:

Sorted

Name: Bowie, David, years in service:5, grade: 8, last review: Improvement Plan

Name: Lee Roth, David, years in service:9, grade: 4, last review: Below Expectations

Name: Garbo, Greta, years in service:1, grade: 3, last review: Outstanding

Name: Kardashian, Khloe, years in service:5, grade: 10, last review: Outstanding

Name: Kardashian, Kim, years in service:15, grade: 9, last review: Meets Expectations

main.cpp

#include #include #include "Record.h"

using namespace std;

void printRecords(vector employees) { for ( auto it:employees) { it->print(); } }

void sortBy(Record::fieldType sortField, vector &employees) { bool sorted = false; while (!sorted) { sorted = true; for (unsigned int i=0; i < employees.size()-1; i++) { if (employees[i]->compare(sortField, employees[i+1]) > 0 ) { Record* temp = employees[i]; employees[i] = employees[i+1]; employees[i+1] = temp; sorted = false; } } } } int main() { std::vector employees; employees.push_back(new Record("David", "Bowie", 5, 8, 0)); employees.push_back(new Record("Kim", "Kardashian", 15, 9, 2)); employees.push_back(new Record("Khloe", "Kardashian", 5, 10, 3)); employees.push_back(new Record("David", "Lee Roth", 9, 4, 1)); employees.push_back(new Record("Greta", "Garbo", 1, 3, 5)); cout<<"Unsorted"<>sortField; switch (sortField) { case 0: sortBy(Record::firstNameField, employees); break; case 1: sortBy(Record::lastNameField, employees); break; case 2: sortBy(Record::yearsInServiceField, employees); break; case 3: sortBy(Record::incomeGradeField, employees); break; case 4: sortBy(Record::reviewPerformanceField, employees); break; default:cout<<"Unknown field"<

Record.h

#include #include #include "Record.h"

using namespace std;

void printRecords(vector employees) { for ( auto it:employees) { it->print(); } }

void sortBy(Record::fieldType sortField, vector &employees) { bool sorted = false; while (!sorted) { sorted = true; for (unsigned int i=0; i < employees.size()-1; i++) { if (employees[i]->compare(sortField, employees[i+1]) > 0 ) { Record* temp = employees[i]; employees[i] = employees[i+1]; employees[i+1] = temp; sorted = false; } } } } int main() { std::vector employees; employees.push_back(new Record("David", "Bowie", 5, 8, 0)); employees.push_back(new Record("Kim", "Kardashian", 15, 9, 2)); employees.push_back(new Record("Khloe", "Kardashian", 5, 10, 3)); employees.push_back(new Record("David", "Lee Roth", 9, 4, 1)); employees.push_back(new Record("Greta", "Garbo", 1, 3, 5)); cout<<"Unsorted"<>sortField; switch (sortField) { case 0: sortBy(Record::firstNameField, employees); break; case 1: sortBy(Record::lastNameField, employees); break; case 2: sortBy(Record::yearsInServiceField, employees); break; case 3: sortBy(Record::incomeGradeField, employees); break; case 4: sortBy(Record::reviewPerformanceField, employees); break; default:cout<<"Unknown field"<

Record.cpp

#include "Record.h"

using namespace std;

Record::Record(std::string first, std::string last, int years, int grade , int review) { firstName = first; lastName = last; yearsInService = years; incomeGrade = grade; reviewPerformance = min(3,review);

} void Record::print() { cout<<"Name: "<

// compares specified field between two Records // returns 0 if equal, // returns <0 if compared object is lesser,>0 if compared object greater int Record::compare(Record::fieldType field, Record* compareRecord) { 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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago