(C++)Write a program that uses several arrays. These are: - empID: an array of i
ID: 3815505 • Letter: #
Question
(C++)Write a program that uses several arrays. These are:
- empID: an array of integers that will hold employee identification numbers. The user will enter the numbers.
- hours: an array of integers to hold the number of hours worked by each employee.
- payRate: an array of doubles to hold the number of hours worked by each employee.
- wages: an array of seven doubles to hold each employee's gross wages.
The program should set an upper bound (MAX_SIZE) for each of the arrays. It should then prompt the user for the number of employees. The program should then use a for loop to have the user enter the employeeID, the payRate, and the number of hours worked for each of the employees.
The program should then calculate the pay for each of the employees following the current overtime rule that says if an employee works more than 40 hours in a week they must receive 1.5 times their hourly payRate for the amount over 40 hours.
The program will then calculate the total number of hours worked, the amount paid, and the average hourly wage that was paid.
The program should print the results in a formatted table.
Each of the arrays, an integer variable for the number of employees, and the variables for the totals are to be in a class object.
*Input validation: do not accept negative values for any of the inputs. The hourly payRate must be no less than the current minimum wage rate of $7.25.
This is the way that the final program is supposed to be laid out. If you could fill in the layout it would be greatly appreciated. If anything in the images is unclear, comment what isn't clear I'll clarify.
Explanation / Answer
Here is the code for you:
//
// Your Name
// CMPSC 121
// Date
// Description
//
// If you choose to use a preprocessor directive - ie to turn debugging
// notes on an off - you would put them here
// #define debug 1
// Pound includes
#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>
using namespace std;
// Set a MAX_SIZE for the arrays - make it a constant
const int MAX_SIZE = 200000000;
// Class Definitions
class Payroll {
public:
// Integer arrays for employee IDs and hours worked
int employeeID[MAX_SIZE];
int hours[MAX_SIZE];
// double arrays for hourly wage and total pay
double hourlyWage[MAX_SIZE];
double totalPay[MAX_SIZE];
// int variable for the number of employees
int size;
// double variable (not an array) for the total amount paid
double grandTotalPaid;
// Any other variables would be considered temporary and can be
// declared when you use them
};
// Function Prototypes
void printHeader(string, string, string, string);
int getInputs(int [], int [], double []); // Input Function
int getSize(void); // Input Function for number of employees
int getID(void); // Input Function for a single employee's ID Number
int getHours(void); // Input Function for a single employee's hours worked
double getWages(void); // Input Function for a single employee's hourly wage
double calcPay(int, double); // Function to calc the individual pay
double calcCompanyPay(double [], int); // Function to calc the total pay
void printResults(Payroll);
bool isInRange(int, int, int); // Check if an integer is in range
bool isInRange(double, double, double); // Check if a double is in range
// Definition of the program function
int main(void) {
// Call the splash screen function
printHeader("Your Name", "CMPSC 121", "Date", "Program Description");
// Create an object to hold the employee input data and output information
Payroll employees;
// Prompt the user for the input data. Both variables are
// passed by reference not value
employees.size = getInputs(employees.employeeID, employees.hours, employees.hourlyWage); // Needs three of the four arrays
// Call appropriate function to enter the data into the arrays
// Call a function to perform the calculations. This includes calculating
// the wages for each employee (remember overtime) and the total pay
// paid out by the company
for(int i = 0; i < employees.size; i++)
employees.totalPay[i] = calcPay(employees.hours[i], employees.hourlyWage[i]);
employees.grandTotalPaid = calcCompanyPay(employees.totalPay, employees.size);
// Call the appropriate function to print the results by passing the
// object
printResults(employees);
return 0;
}
int getInputs(int id[], int hours[], double hourlyWage[]) {
// GETINPUTS getInputs(int id[], int hours[], double hourlyWage[])
// is a wrapper function used to enter the data for the three arrays.
//
// The arrays are passed to the function (not the object)
//
// This is a wrapper function to simply call the individual input functions
//
// Name:
// Course: CMPSC 121
// Date:
//
// This function returns an int for the number of elements in the array
// Create a for loop that runs from 0 to one less than the number
// of employees
// Each run of the loop calls the function to get the employee ID,
// the hourly wage, and the number of hours worked
int numOfEmployees = getSize();
for(int i = 0; i < numOfEmployees; i++)
{
id[i] = getID();
hours[i] = getHours();
hourlyWage[i] = getWages();
}
return numOfEmployees;
}
int getSize(void) {
// GETN getN(void)
// is the recursive function used to enter the data for the number of
// employees.
//
// Name: Prof. Adams
// Course: CMPSC 121
// Date: 7 April 2017
//
// Call a function to enter n
// This function has no input parameters and returns n
int n; // Number of employees
cout << "Enter the number of employees: ";
cin >> n;
// Check if the value is at least 0. If not it calls itself recursively
bool goodValue = false;
goodValue = isInRange(n, 1, 200000000);
// if - else as a stopping case
if(!goodValue) {
// Print an error message
cout << "You must have at least one employee. ";
n = getSize();
}
return n; // Returns n
}
int getID(void) {
// GETID getID(void)
// is the recursive function used to enter the data for the employee
// number.
//
// Name:
// Course: CMPSC 121
// Date:
//
cout << "Enter the employee ID: ";
int empID;
cin >> empID;
// Check if the value is at least 0. If not it calls itself recursively
bool goodValue = false;
goodValue = isInRange(empID, 1, 200000000);
// if - else as a stopping case
if(!goodValue) {
// Print an error message
cout << "You must have an employee id in range 1 - 200000000. ";
empID = getID();
}
return empID; // Returns employee ID number
}
int getHours(void) {
cout << "Enter the number of hours worked: ";
int hours;
cin >> hours;
// Check if the value is at least 0. If not it calls itself recursively
bool goodValue = false;
goodValue = isInRange(hours, 1, 100);
// if - else as a stopping case
if(!goodValue) {
// Print an error message
cout << "You must have hours in the range 1 - 100. ";
hours = getHours();
}
return hours; // Returns employee ID number
}
double getWages(void) {
cout << "Enter the number of hourly wage: ";
double wage;
cin >> wage;
// Check if the value is at least 0. If not it calls itself recursively
bool goodValue = false;
goodValue = isInRange(wage, 7.25, 100.0);
// if - else as a stopping case
if(!goodValue) {
// Print an error message
cout << "You must have wage in range 7.25 - 100. ";
wage = getWages();
}
return wage; // Returns employee ID number
}
void printHeader(string name, string course, string dueDate, string description) {
// PRINTHEADER printHeader(string, string, string, string) is the
// function used to hide away the splash screen
//
// Name: Prof. Adams
// Course: CMPSC 121
// Date: 12 March 2017
//
// Print the splash screen
cout << endl;
cout << name << endl;
cout << course << endl;
cout << dueDate << endl;
cout << description << endl;
cout << endl;
return;
}
double calcPay(int hours, double hourlyWage) {
// CALCPAY calcPlay(int hours, double hourlyWage) is the
// function used to calculate a single employees pay
//
// Name:
// Course: CMPSC 121
// Date:
//
double pay = hours * hourlyWage;
if(hours > 40)
pay += (hours - 40) * hourlyWage / 2.0;
// return the pay
return pay;
}
void printResults(Payroll employeeData) {
// PRINTRESULTS printResults(Payroll employeeData) is the
// function used to print the results of the program. It is
// passed a copy of the object currently holding the arrays and
// number of employees.
//
// Name:
// Course: CMPSC 121
// Date:
//
// This function should print the input data
// and the results
for(int i = 0; i < employeeData.size; i++)
{
cout << employeeData.employeeID[i] << " " << employeeData.hours[i] << " ";
cout << employeeData.hourlyWage[i] << " " << employeeData.totalPay[i] << endl;
}
// Does not return anything - thus no variable here
return;
}
bool isInRange(int inData, int min, int max) {
// ISINRANGE isInRangle(int inData, int min, int max)
// is a boolean function that checks if a value is within the limits
// The inData is the value to be checked while min and max are the
// range. If the data is within the range the function returns true
// while if it is outside of the range it returns false
//
// This is an overloaded function
//
// Name: Prof. Adams
// Course: CMPSC 121
// Date: 30 March 2017
//
bool inRange = 0; // Set to false as a failsafe
bool s = (inData < min);
bool t = (inData <= max);
// Check inRange or not using the step function approach
inRange = s * t * 0 + (1-s) * t * 1 + (1-s) * (1-t) * 0;
return inRange;
}
bool isInRange(double inData, double min, double max) {
// ISINRANGE isInRangle(double inData, double min, double max)
// is a boolean function that checks if a value is within the limits
// The inData is the value to be checked while min and max are the
// range. If the data is within the range the function returns true
// while if it is outside of the range it returns false
//
// This is an overloaded function
//
// Name: Prof. Adams
// Course: CMPSC 121
// Date: 30 March 2017
//
bool inRange = 0; // Set to false as a failsafe
bool s = (inData < min);
bool t = (inData <= max);
// Check inRange or not using the step function approach
inRange = s * t * 0 + (1-s) * t * 1 + (1-s) * (1-t) * 0;
return inRange;
}
double calcCompanyPay(double individualPays[], int size) {
double sum = 0.0;
for(int i = 0; i < size; i++)
sum += individualPays[i];
return sum;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.