Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help with this coding exercise in C++ -------------------------------------------------- employee_helper.cpp #include #include #include #include #include using namespace std; const int NEWLINE_LENGTH = 2; const int

please help with this coding exercise in C++
--------------------------------------------------
employee_helper.cpp
#include
#include
#include
#include
#include
using namespace std;
const int NEWLINE_LENGTH = 2;
const int RECORD_SIZE = 30 + 10 + NEWLINE_LENGTH;
/**
A basic employee class that is used in many examples
in the book "Computing Concepts with C++ Essentials"
*/
class Employee
{
public:
/**
Constructs an employee with empty name and no salary.
*/
Employee();
/**
Constructs an employee with a given name and salary.
@param employee_name the employee name
@param initial_salary the initial salary
*/
Employee(string employee_name, double initial_salary);
/**
Sets the salary of this employee.
@param new_salary the new salary value
*/
void set_salary(double new_salary);
/**
Gets the salary of this employee.
@return the current salary
*/
double get_salary() const;
/**
Gets the name of this employee.
@return the employee name
*/
string get_name() const;
private:
string name;
double salary;
};
Employee::Employee()
{
salary = 0;
}
Employee::Employee(string employee_name, double initial_salary)
{
name = employee_name;
salary = initial_salary;
}
void Employee::set_salary(double new_salary)
{
salary = new_salary;
}
double Employee::get_salary() const
{
return salary;
}
string Employee::get_name() const
{
return name;
}
/**
Converts a string to a floating-point value, e.g.
"3.14" -> 3.14.
@param s a string representing a floating-point value
@return the equivalent floating-point value
*/
double string_to_double(string s)
{
istringstream instr(s);
double x;
instr >> x;
return x;
}
/**
Reads an employee record from a file.
@param e filled with the employee
@param in the stream to read from
*/
void read_employee(Employee& e, istream& in)
{
string line;
getline(in, line);
if (in.fail()) return;
string name = line.substr(0, 30);
double salary = string_to_double(line.substr(30, 10));
e = Employee(name, salary);
}
int main()
{
fstream fs;
fs.open("employee.txt");
fs.seekg(0, ios::end); // Go to end of file
int nrecord = fs.tellg() / RECORD_SIZE;
// fill in your own code
// ...
// ...
fs.close();
return 0;
}
--------------------------------------------------
employee.txt
Johnson, Marianne 36118.95
Sanchez, Linda 31695.00
Lee, John 32798.90
Baldassarian, George S. 27189.75
Karamanlaki, Alexander 27508.95
Schmidt, Otto 29316.00
Stroustrup, Bjarne 124495.00
Keller, Chris 23318.04
Carter 3rd, Hodding 26632.00
Techasaratoole, Shirley 25795.00
mergesort.cpp
/**
Merges two adjacent ranges in a vector
@param a the vector with the elements to merge
@param from the start of the first range
@param mid the end of the first range
@param to the end of the second range
*/
void merge(vector& a, int from, int mid, int to)
{
int n = to - from + 1; // Size of the range to be merged
// Merge both halves into a temporary vector b
vector b(n);
int i1 = from;
// Next element to consider in the first half
int i2 = mid + 1;
// Next element to consider in the second half
int j = 0; // Next open position in b
// As long as neither i1 nor i2 is past the end, move the smaller
// element into b
while (i1
{
if (a[i1]
{
b[j] = a[i1];
i1++;
}
else
{
b[j] = a[i2];
i2++;
}
j++;
}
// Note that only one of the two while loops below is executed
// Copy any remaining entries of the first half
while (i1
{
b[j] = a[i1];
i1++;
j++;
}
// Copy any remaining entries of the second half
while (i2
{
b[j] = a[i2];
i2++;
j++;
}
// Copy back from the temporary vector
for (j = 0; j
a[from + j] = b[j];
}
/**
Sorts the elements in a range of a vector.
@param a the vector with the elements to sort
@param from start of the range to sort
@param to end of the range to sort
*/
void merge_sort(vector& a, int from, int to)
{
if (from == to) return;
int mid = (from + to) / 2;
// Sort the first and the second half
merge_sort(a, from, mid);
merge_sort(a, mid + 1, to);
merge(a, from, mid, to);
}
image text in transcribed
Problem 2 (50pt): Modify the merge sort algorithm to sort a vector of employees by salary. The program constructs a vector of employees reading from file employee.txt and sort the vector in ascending order by salary using merge sort algorithm. Print the sorted vector to the console. Modify helper functions employee helper.cpp and mergesort.cpp uploaded on CCLE and write other necessary functions to make the output look exactly like the sample as follows. Instructions: (5pt) Put all your code in one cpp file, named employeesort.cpp, and submit it to ccle.ucla.edu. Write your code with good coding practices. Comment on your code to make it readable and add description of files in the beginning to show your ownership. (45pt) Compile your code and run your program to check for compile-time errors and logic errors. Your output should follow the format as follows. Remember to check your homework with PIC lab desktop if you don't code with VS 2019. You may lose the majority of points if your code doesn't compile. Keller, Chris Techasaratoole, Shirley Carter 3rd, Hodding Baldassarian, George S. Karamanlaki, Alexander Schmidt, Otto Sanchez, Linda Lee, John Johnson, Marianne Stroustrup, Bjarne 23318 25795 26632 27189.8 27509 29316 31695 32798.9 36118.9 124495

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions