228 AM L 68 ooo Sprint HW 12 CS1337 Homework 12 Jan 12 due J

2:28 AM L 68% ooo Sprint HW 1-2 CS1337 Homework 1-2 Jan 12, due Jan 24 at 11:S9 PM This homework assignment gives you the opportunity to practice seanchingsoningamaysandfile Wo. HW1-2 (Graded out of 100): Wage Calculation Your program will calculate employees\' wages from data read troma fue combined with datainpunted by the user. The data inthe fle consists ofemployee IDs and employees\' hourly rates. The program wil read the file and store the employee IDs and hourlyrates in paralelamays. The program promptsthe uiertotype in an employee ID, the number of hours worked by that employee, and wearches fora match in the employee Darray. If a matchis found, the program displays the hourly rate, calculates and displays the wage The wage is calculated as houry rate\" number of hours worked The program performs input validation: the employee ID must be m1 and e NUMARANGE, and thenumber of houry must be u0.lt the input is invalid, the user ispromptedto reenter the data. Your program must implement the following functions get Data: This function takes as arguments an ID array and a rate amay, reads hom the input file and populates the parallelanray The functionreturns the actual number of employees read from the file. If the file cannot be opened your program should output an entor message and eit sea rehi This function takes as arguments an employee o anamay, the actualnumber of employees, and searches the array for a match. The function returns the index of the element where a matchis found, or-1 rno match is found. For this assignment, implement the linear search You are encouraged to implement additional functions to make your program more modular. You can assume there are at most 1.000 employee records inthe file, and set the array saesto 1.000 However, the actual number of emploweesldetermined byvour getData functionl less or equal to You can assume that each employee has a unique ID and the possible Dvalues range from 1to NUMLRANGE 999999 inclusive. mployee ID and rate are of type sat You can hard code the flename:\"employeefile .The file is posted on elearning c) outline of main Your main function must demonstrate your get Data and seareh functions are working comenty by implementing this pseudocode: while user does not choose Quit Prompt the user toinput an employee Dond number hours-Performinput voldotion Depending on the outcome, print the emplovee\'s rateond woge, or Emplowee nor found .The

Solution

#include <stdio.h>
#include<stdlib.h>

int getData(int *employee, int *hour_rate)
{
FILE *fp;

fp = fopen(\"data.txt\", \"r\");

if (fp == NULL)
exit(EXIT_FAILURE);
int emp, hour;
int i = 0;
while (! feof(fp)) {
i++;
fscanf(fp,\"%d %d\", &emp, &hour);

}

int j = 0;
fclose(fp);
fp = fopen(\"data.txt\", \"r\");
for(j = 1; j < i-1;j++)
{
fscanf(fp,\"%d %d\", &emp, &hour);
employee[j] = emp;
hour_rate[j] = hour;
}
return j;
}

int search(int *employee, int employee_size, int employee_id, int *number_of_comparison)
{
int i = 0;
for(i = 0; i < employee_size; i++)
{
*number_of_comparison = *number_of_comparison + 1;
if (employee[i] == employee_id)
{
return i;
}
}

return -1;
}

int main()
{
int employees[1000], hour_rate[1000];
int number_of_employee = getData(employees, hour_rate);

int max_id = 999999;


printf(\"Actual number of employees = %d\ \", number_of_employee);
printf(\"---------------------------------------------\ \");
int choice;
int hour;
while(1)
{
printf(\"Enter employee\'s ID. 0 to quit: \");
scanf(\"%d\", &choice);
if (choice == 0)
break;

if (choice > max_id) {
printf(\"Please enter a valid employee id\ \");
continue;
}

printf(\"Enter number of hours: \");
scanf(\"%d\", &hour);
if (hour <= 0)
{
printf(\"Please enter valid value for number of hours.\ \");
}
int comparison_count = 0;
int index = search(employees, number_of_employee, choice, &comparison_count);
if (index != -1)
{
printf(\"Index is : %d\ \", index);
int wage = hour_rate[index]*hour;
printf(\"Employee: %d, rate = %d, hours = %d, wage = %d\ \", choice, hour_rate[index], hour, wage);
}
else
{
printf(\"Employee not found\ \");
}
printf(\"It took %d comparisons\ \", comparison_count);
}

return 0;
}

 2:28 AM L 68% ooo Sprint HW 1-2 CS1337 Homework 1-2 Jan 12, due Jan 24 at 11:S9 PM This homework assignment gives you the opportunity to practice seanchingsoni
 2:28 AM L 68% ooo Sprint HW 1-2 CS1337 Homework 1-2 Jan 12, due Jan 24 at 11:S9 PM This homework assignment gives you the opportunity to practice seanchingsoni

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site