In: Computer Science
C++
This exercise will provide practice with working with structures and arrays. Although there might be many ways of doing the assignment, to get full credit, it must be done exactly as specified here(for purpose of practice)
Write a program that will record information about employees and will compute their paychecks. Each employee must be represented by a struct containing the last name, hours worked each day of the week, the hourly rate, and the pay for the week.
Hours worked each day of the week must be and array storing 5 values for the hours from Monday to Friday( The company is not open on Saturday or Sunday). This array will be inside the structure.
There will also be an array of the employees; array of structures. There must be at least four (4) employees. The array must be declared in the main function of the program. Then the entire array must be passed to a function called 'initialize'. This function will ask the user to enter values for every field of every structure, except for the pay for that week. ( the pay will be computed in a different function)
A loop for processing the array of employees must be set up in the main function. Inside the loop, a single employee will be past to the function called 'compute', which will calculate the check for that employee. If the employee worked longer than 40 hours, overpay of 1.5 times the hourly rate is to be used for each hour worked over the 40 hours. For example, an employee who worked a total of 50 hours for $10.00 an hour would make $550.00. Don't pass the entire array of employees into the 'compute'; the employees will be passed one at a time into the function, until all of their paychecks have been calculated, at which time the loop will terminate. The employee will need to be passed by reference.
A single employee must be passed into the result function, which will output the last name of the employee. Do not pass the entire array of employees to the 'result' function. Each employee must be passed through call by value, one at a time, until all of the paycheck amount have been output at which time the loop in the main function will terminate. Then the program will end.
Screenshot
-------------------------------------------------------------------------------------
Program
//Header files
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
//Create a structure for employee
struct Employee {
string lastName;
double hoursWorked[5];
double hourlyRate;
double payment;
};
//Function prototypes
void compute(Employee &employee);
void initialize(Employee *employee,int size);
void result(Employee employee);
//Test
int main()
{
//Create an employee struct array of size 4
Employee emp[4];
//Get employee details
initialize(emp,4);
//Compute their week payment
for (int i = 0; i < 4; i++) {
compute(emp[i]);
}
//Display result
for (int i = 0; i < 4; i++) {
result(emp[i]);
}
return 0;
}
/*
//Method to initialize employee details
//Employee array and it's size as parameters
//Set employee details otherthan payment
*/
void initialize(Employee *employee, int size) {
for (int i = 0; i < size; i++) {
Employee emp;
cout << "Enter the last name
of employee: ";
cin >>
employee[i].lastName;
for (int j = 0; j < 5; j++)
{
cout <<
"Enter " << employee[i].lastName << "'s working hour
Day"<<(j+1)<<": ";
cin >>
employee[i].hoursWorked[j];
}
cout << "Enter " <<
employee[i].lastName << "'s hourly rate: ";
cin >>
employee[i].hourlyRate;
}
}
/*
//Method to comput payment
//Take each employee's details as parameter as reference
//Then find total working hours
//Then calculate payment and set
*/
void compute(Employee &employee) {
employee.payment = 0.0;
double hrsWorked = 0;;
for (int i = 0; i < 5; i++) {
hrsWorked +=
employee.hoursWorked[i];
}
if (hrsWorked <= 40) {
employee.payment += hrsWorked*
employee.hourlyRate;
}
else {
employee.payment += (40 *
employee.hourlyRate) + ((hrsWorked -
40)*(1.5*employee.hourlyRate));
}
}
/*
//Method to display each employee's payment details
//Take employee as parameter as value
//Display last name and pay check amount in 2 decimal places.
*/
void result(Employee employee) {
cout << fixed << setprecision(2);
cout << "LastName: " << employee.lastName
<< " PayCheck: $" <<
employee.payment << endl;
}
------------------------------------------------------------------------
Output
Enter Milan's working hour Day3: 8
Enter Milan's working hour Day4: 7
Enter Milan's working hour Day5: 6
Enter Milan's hourly rate: 10
Enter the last name of employee: Moke
Enter Moke's working hour Day1: 10
Enter Moke's working hour Day2: 9
Enter Moke's working hour Day3: 10
Enter Moke's working hour Day4: 9
Enter Moke's working hour Day5: 10
Enter Moke's hourly rate: 15
Enter the last name of employee: Albert
Enter Albert's working hour Day1: 9
Enter Albert's working hour Day2: 8
Enter Albert's working hour Day3: 9
Enter Albert's working hour Day4: 8
Enter Albert's working hour Day5: 9
Enter Albert's hourly rate: 12
Enter the last name of employee: Mayer
Enter Mayer's working hour Day1: 12
Enter Mayer's working hour Day2: 10
Enter Mayer's working hour Day3: 9
Enter Mayer's working hour Day4: 9
Enter Mayer's working hour Day5: 7
Enter Mayer's hourly rate: 11
LastName: Milan PayCheck: $400.00
LastName: Moke PayCheck: $780.00
LastName: Albert PayCheck: $534.00
LastName: Mayer PayCheck: $555.50
Press any key to continue . . .