Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Concepts tested by the program: Working with arrays Using file operations 3.

ID: 3835567 • Letter: C

Question

C++ Concepts tested by the program: Working with arrays Using file operations 3. Using a selection sort to sort parallel arrays 4. Using a binary search with sorted arrays 5. Using a sequential (linear) search with unsorted arrays 6. Implementing functions besides function main() Description Write a C++ program that processes a payroll. Your program will calculate the wages for each employee, given the number of hours worked and the payrate per hour. It will search for two employees by number, one with a binary search (see page 462) and one with a linear search (see page 459). A selection sort (see page 476) will sort the arrays so that the employee numbers are in ascending order. Specifications Input for this code has two sources. The user must enter the names of the input and output files from the keyboard. The user will enter two employee numbers from the keyboard in response to prompts. The input file should have one line for each employee with the 3 required items: employeeID, hoursWorked, and payRate (separated by whitespace on the file) The last line of the file must always have only -1. Output also has two sources. The program title and the programmer name should appear on both the console and the file. If the input file does not open, an error message should appear on the console and no attempt should be made to open the output file for any output or processing. The message "Processing complete" should appear on the console after the programmer name and just before the main program ends (make it the last statement before "return;"). The console output will show the prompts with the search numbers. The file output should show the unsorted table of the input items with the wages, followed by the sorted table with the same 4 columns. The column numbers with decimal amounts should line up on the decimal point under the column headings. Processing Requirements Your program should use the following one-dimensional arrays: empId: an array of long integers to hold employee identification numbers. Assume there will be no more than 20 identification numbers, but your code should check that the file doesn't attempt to enter more. If an attempt is made to enter 21, process only the first 20. hours: an array of doubles to hold the number of hours worked by each employee. Fractional hours are possible. payRate: an array of doubles to hold each employee's hourly pay rate. wages: an array to hold each employee's gross wages. The program should relate the data in each array through the subscripts. For example, the number in element 0 of the hours array should be the number of hours worked by the employee whose identification number is stored in element 0 of the empId array. That same employee's pay rate should be stored in element 0 of the payRate array. 1) Calculate the wages for each employee and place into an array with the related subscript. 2) Write the unsorted arrays on the output file. 3) Prompt for and input from the keyboard an empID to use for the linear search. 4) Next sort the empID long integers using the selection sort algorithm. If an exchange is made for empID, be sure to make the corresponding exchanges in the three associated items. 5) Write the sorted arrays on the output file. 6) Prompt for and input from the keyboard an empID to use for the binary search. The Selection sort algorithm, Binary Search and Linear Search algorithms should be three separate functions that will be called from the function main(). Modify the code in your text for this project. Do NOT use any output statements in these functions. You may use more functions in your design. You may assume that the input file is constructed correctly, and will always contain -1 at the end. Be sure to test for an empty file (one that contains ONLY -1) and a file with more than 20 employees. Print an error message to the screen if there are no employees, and do NOT attempt to open the output file. If your file has more than 20 employees, just process the first 20 (without an error message.) To input the file items without reading past the end of the file (-1 is the sentinel), use a while loop with two conditions: the empID in a temporary variable that is not negative and the number of employees less than 20. Inside your loop body: Store the "good" employee number, input the number of hours and the payRate in the arrays. The last item in your loop should be the next employee number from the file. Sample input file: oft Visual Studio Express 2012 for Wind PROJECT UILD TEAM TOOLS TEST WINDOW H M Local Windows Debugger Debug Win32 n7.tot 658845 46 8 0125 25.25 9.2 7895122 30 1 8451277 50 10.20 1382850 35.20 7.50 Output 5how output from: Build Solution Explo Search Solution Explorer Solution ConsoleApplication 13 a project CancalcApplikation13 D FH External Dependencies Header Files Resource Files S Source Files D FayToll.cpp Col] Ch] 2:53 PM 1/26/2017 Console output: Output: Your output should be in the format shown below: A flowchart showing your main function logic. Use a striped rectangle containing the name of the function for each function call.

Explanation / Answer

Here is the code for the question. Please do change the programmer name in the file . It is represented as XXXX. Also don't forget to rate the answer if it helped. Thank you very much

payroll.cpp

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

//loads a maximum of 20 records from input file into 3 parallel arrays and returns the count of number of records loaded
int load_file(ifstream &infile,long empids[], double hours[], double payrate[])
{
long id;
int count = 0;
while(infile >> id && count < 20)
{
if(id == -1) //no more records
break;

empids[count] = id;

infile>>hours[count] >> payrate[count];
count++;
}
return count;
}

//calculates and store wages for all n records
void calculate_wages(double hours[], double payrate[], double wages[], int n)
{
for(int i=0; i < n; i++)
wages[i] = hours[i] * payrate[i];
}

//writes the arrays into an output stream
void write_to_file(ostream &outfile, long empids[], double hours[], double payrate[], double wages[], int n)
{
outfile<<"Emp Id Hours Rate Wages"<<endl;
outfile<<"__________________________________________"<<endl<<endl;
for(int i = 0 ;i < n; i++)
{
outfile<<setw(15)<<left<<empids[i];
outfile<<setw(10)<<left<<fixed<<setprecision(2)<<hours[i];
outfile<<setw(10)<<payrate[i];
outfile<<setw(10)<<setprecision(2)<<wages[i]<<endl;
}
outfile<<endl;
}

//performs linear search for employee id and returns the corresponding index if found and -1 if not found
int linear_search(long empids[], long id, int n)
{
for(int i=0; i<n; i++)
{
if(empids[i] == id)
return i;

}
return -1;
}

//performs binary search for employee id and returns the corresponding index if found and -1 if not found
int binary_search(long empids[], long id, int n)

{
int start = 0 ,end =n-1,mid;
while(start<=end)
{
mid = (start+end)/2;
if(empids[mid ]== id)
return mid;
if(id < empids[mid])
end = mid-1;
else
start = mid+1;
}

return -1;
}
//selection sort on employee id
void selection_sort(long empids[], double hours[], double payrate[], double wages[], int n)
{
long tid;
double thour, trate, twage;
int minidx;
for(int i =0 ;i<n; i++)
{
minidx = i;
for(int j=i+1; j<n; j++)
{
if(empids[j] < empids[minidx])
minidx = j;
}

if(minidx != i)
{
//swap

tid = empids[i];
thour = hours[i];
trate = payrate[i];
twage = wages[i];

empids[i] = empids[minidx];
hours[i] = hours[minidx];
payrate[i] = payrate[minidx];
wages[i] = wages[minidx];

empids[minidx] = tid;
hours[minidx] = thour;
payrate[minidx] = trate;
wages[minidx] = twage;

}
}
}

int main()
{
long empids[20];
double hours[20], payrate[20], wages[20];
int count;

long id;

string in_filename, out_filename;
string programmer="XXXXXX"; //name of the programmer
ifstream infile;
ofstream outfile;


cout<<"Enter the input filename: ";
cin>>in_filename;

infile.open(in_filename.c_str());
if(infile.fail())
{
cout<<"Error opening input file "<<in_filename<<"! "<<endl;
return 1;
}


cout<<"Enter the output filename: ";
cin>>out_filename;

count = load_file(infile, empids, hours, payrate);
infile.close();

if(count == 0)
{
cout<<"No data is loaded"<<endl;

return 1;
}

outfile.open(out_filename.c_str());
if(outfile.fail())
{
cout<<"Error opening output file "<<out_filename<<"! "<<endl;
return 1;
}

cout<<"Program demonstrating arrays, file operations, sorting and searching algorithms - "<<programmer<<endl<<endl;
outfile<<"Program demonstrating arrays, file operations, sorting and searching algorithms - "<<programmer<<endl<<endl;

//calculate wages
calculate_wages(hours, payrate, wages,count);

//write unosrted arrays into output file
outfile<<"The unsorted arrays are as follows :"<<endl<<endl;
write_to_file(outfile, empids, hours, payrate, wages, count);


//linear search
cout<<"Enter an employee id for linear search: "<<endl;
cin>>id;
int idx = linear_search(empids, id, count);
if(idx == -1)
cout<<"No employee with ID: "<<id<<endl;
else
{
cout<<"Found employee with Id: "<<id<<" hours: "<<hours[idx] <<" payrate: "<<payrate[idx]<<" wages: "<<wages[idx]<<endl;
}

//sort on empids
selection_sort(empids, hours, payrate, wages, count);

//write sorted arrays into output file
outfile<<"The sorted arrays (based on employee id )are as follows :"<<endl<<endl;
write_to_file(outfile, empids, hours, payrate, wages, count);


//binary search
cout<<"Enter an employee id for binary search: "<<endl;
cin>>id;
idx = binary_search(empids, id, count);
if(idx == -1)
cout<<"No employee with ID: "<<id<<endl;
else
{
cout<<"Found employee with Id: "<<id<<" hours: "<<hours[idx] <<" payrate: "<<payrate[idx]<<" wages: "<<wages[idx]<<endl;
}

outfile.close();
cout<<"Processing complete."<<endl;
}

input file employees.txt

658845 46 8
125 25.25 9.2
7895122 30 1
8451277 50 10.20
1382850 35.20 7.50

console output

Enter the input filename: employees.txt
Enter the output filename: pay.txt
Program demonstrating arrays, file operations, sorting and searching algorithms - XXXXXX

Enter an employee id for linear search:
1382850
Found employee with Id: 1382850   hours: 35.2   payrate: 7.5   wages: 264
Enter an employee id for binary search:
7895122
Found employee with Id: 7895122   hours: 30   payrate: 1   wages: 30
Processing complete.

output file pay.txt

Program demonstrating arrays, file operations, sorting and searching algorithms - XXXXXX

The unsorted arrays are as follows :

Emp Id            Hours    Rate    Wages
__________________________________________

658845 46.00 8.00 368.00
125 25.25 9.20 232.30
7895122 30.00 1.00 30.00   
8451277 50.00 10.20 510.00
1382850 35.20 7.50 264.00

The sorted arrays (based on employee id )are as follows :

Emp Id            Hours    Rate    Wages
__________________________________________

125 25.25 9.20 232.30
658845 46.00 8.00 368.00
1382850 35.20 7.50 264.00
7895122 30.00 1.00 30.00   
8451277 50.00 10.20 510.00