C++ Program file content 00000042 87 00006335 40 00019170 64 00011479 98 0002696
ID: 3782411 • Letter: C
Question
C++ Program
file content
00000042 87 00006335 40 00019170 64 00011479 98 00026963 84
00005706 85 00023282 47 00009962 31 00002996 42 00004828 96
00032392 64 00003903 93 00000293 82 00017422 96 00019719 75
00005448 66 00014772 38 00001870 92 00025668 79 00017036 74
00028704 71 00031323 33 00017674 44 00015142 51 00028254 88
00025548 64 00032663 57 00020038 79 00008724 81 00027530 78
00012317 95 00022191 22 00000289 46 00009041 82 00019265 28
00027447 65 00015891 29 00024371 90 00015007 81 00024394 48
00019630 83 00024085 54 00018757 20 00004967 36 00013932 88
00016945 59 00024627 63 00005538 38 00016119 22 00022930 81
00004834 95 00004640 78 00022705 30 00013978 86 00031674 86
00005022 45 00026925 52 00006271 89 00026778 73 00005098 52
00023987 30 00009162 96 00022356 67 00023656 74 00004032 72
00027351 50 00016942 64 00013967 90 00031108 51 00018008 77
00015458 67 00027754 83 00014946 49 00032210 98 00024222 48
00006423 86 00027507 90 00016414 68 00000901 51 00018763 75
00017411 59 00027625 77 00021549 23 00027596 61 00003603 50
00010292 56 00009375 80 00004597 41 00027349 99 00019669 24
00008282 34 00000054 99 00026419 38 00006901 48 00018128 87
00003729 33 00024649 23 00017808 41 00014311 77 00022814 94
User Input
Your program will calculate employees' wages from data read from a file combined with data inputted by the user. The data in the fie consists of employee IDs and employees' hourly rates. The program wa read the file and store the employee IDs and hourly rates inparallel arrays. The program prompts the to emplovee the number of hours worked that and searches for a match in the employee array a is program displays the hourly rate, calculates and displays the wage. The wage is calculated as heuriv rate number of hours worked. The program performs input validation: the employee ic must be >-1 and NUM RANGE, and the number of hours must be smo, if the input is invald, the user is prompted to reenter the data. 1. Requirements a) Functions Your program must implement the following getData: This function takes as arguments an ID array and a rate array, reads from the input fil11e and populates the parallel arrays The function returns the actual number of employees read from the file. If the file cannot be opened, vour program should output an error message and exit. search: This function takes as arguments an employee ID, an array, the actual number of emplayees, and searches the array for a match. The function returns the index of the element where a match is found, or -1 if no match is found. For this assignment, implement the linear search. o make your program more modular You are encouraged to implement additional functions b) Assumptions s in the file, and set the array sizes to 1,000. You can assume there are at most 1,000 employee records However, the actual number of employ (determined by your getData function) is less or equal to You can assume that each employee has a unique ID, and the possible ID values range from to NUM RANGE 999999 inclusive. Employee ID and rate are of type int employee File tot". The file is posted on elearning You can hard code the file name c) Outline of main Your main function must demonstrate your getData and search functions are working correctly by implementing this pseudo code: while (user does not choose QuitExplanation / Answer
Following is the required code. Hope it helps:
//Employee wages
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
#define NUM_RANGE 999999
//function prototype
int getData(int id[], int rate[]);
int Search(int emp_id, int id[], int num_of_emp);
int main()
{
int id[1000],rate[1000];
int num_of_emp, emp_id, hr,index,res;
char ch;
num_of_emp = getData(id,rate);
do
{
cout<<"Enter employee id and no. of hours";
cin>>emp_id>>hr;
//sanity check
if(emp_id>=1 && emp_id<=NUM_RANGE && hr>=1)
{
index = Search(emp_id, id, num_of_emp);
if(index != -1)
{
res = rate[index]*hr;
cout<<"Emp. rate: "<<rate[index]<<endl<<"Result:"<<res;
cout<<"No. of searches: "<<index+1;
}
else
{
cout<<"Employee is not found";
}
}
else
continue;
cout<<"Press 'y' or 'Y' to enter more data:";
cin>>ch;
}while(ch=='y' || ch=='Y');
return 0;
}//end of main function
int getData(int id[], int rate[])
{
int i = 0;
ifstream myfile ("employeeFile.txt");
if (myfile.is_open())
{
while(true)
{
myfile >> id[i] >> rate[i];
i++;
if( myfile.eof() )
break;
}//end of while loop
}
else
{
cout<<"Unable to open file";
exit(1);
}
return i;
}//end of getData function
int Search(int emp_id, int id[], int num_of_emp)
{
int i;
for(i=0;i<num_of_emp;i++)
{
if(id[i] == emp_id)
return i;
}
return -1;
}//end of search function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.