Write a C program that will calculate the gross pay of a set of employees. Decla
ID: 3678114 • Letter: W
Question
Write a C program that will calculate the gross pay of a set of employees. Declare an array of structures to hold your employees as well as a pointer to it.
Do not use any array references with indexes. For example:
emp[i].wage /* this is bad, it uses an array reference with an index, in this case, i */
emp_ptr->wage; /* this is good, it uses a pointer to reference the wage value */
The program determines the overtime hours (anything over 40 hours), the gross pay and then outputs a table in the following format.
Column alignment, leading zeros in Clock#, and zero suppression in float fields is important.
Use 1.5 as the overtime pay factor.
You should implement this program using a structure similar to the suggested one below to store the information for each employee. Feel free to tweak it if you wish. For example, its OK to have a first and last name member instead of just a name member, and if you want to use different types (such as long or double), that is OK as well.
struct employee
{
char name [20];
int id; /* can be long int if you wish */
float wage;
float hours;
float overtime;
float gross;
};
Use the following information to initialize your data.
Connie Cobol 98401 10.60
Mary Apl 526488 9.75
Frank Fortran 765349 10.50
Jeff Ada 34645 12.25
Anton Pascal 127615 10.00
Create an array of structures with 5 elements, each being of type struct employee. If you wish, you can break up the name member into first and last name members.
Initialize the array with the data provided. Prompt for employee hours, and calculate overtime and gross pay. Continue to design and implment various functions ... build upon what you have learned.
Set a pointer to it and then use that pointer going forward to access elements (and their associated members) in your array of structures. Again, do not use array references with indexes (use emp_ptr->hours ... not ... emp [ i ].hours as the latter is not a fast).
Similar to your previous homework:
1) These are mandatory and need to be done:
a) Add a Total row at the end to sum up the hours, overtime, and gross columns
b) Add an Average row to print out the average of the hours, overtime, and gross columns
2) These two optional challenges if you have time
a) Calculate and print the minimum hours, overtime, and gross values
b) Calculate and print the maximum hours, overtime, and gross values
This is my code so far please help and explain because it doesn't compile atm. I also need help on calculating the total row and average.
/************************************************************************/
/* */
/* HOMEWORK: 7 */
/* */
/* Nam */
/* */
/ercourse */
/* */
/r /> /* */
/* Description: Program which determines gross pay based on overtime */
/* and outputs a formatted answer. Employee information */
/* is stored in an array of structures and referenced */
/* through the use of pointers. */
/************************************************************************/
#include
#include
/* define all constants here */
#define SIZE 5
#define OVTP 1.5f
#define STD_HOURS 40.0f
/* type to hold employee information */
struct employee
{
char name [20]; /* Employee first and last name */
int id; /* unique employee identifier */
float wage; /* hourly wage rate */
float hours; /* hours worked in a given week */
float overtime; /* hours worked after the standard work week */
float gross; /* total gross pay, standard pay + overtime pay */
};
/* add function prototypes here if you wish */
// *******************************************************************
// Function get_hours
//
// Purpose: This function will get the hours worked for a given
// number of employees. It will save each value into an
// array.
//
// Parameters: employees - the array of structures for employees
// size - the number of employees to input
//
// Returns: Nothing (void)
//
// *******************************************************************
void getHours (struct employee *emp_ptr, int size)
{
// Request the number of hours worked for each employee
printf("Please enter the number of hours worked for employee %s: ", emp_ptr->name);
scanf("%f", &emp_ptr->hours);
}
// *******************************************************************
// Function calculate_overtime
//
// Purpose: This function will calculate the number of overtime
// hours worked by a given number of employees. It will
// save each value into an array.
//
// Parameters: employees - the array of structures for employees
// size - the number of employees to input
//
// Returns: Nothing (void)
//
// *******************************************************************
void calculate_overtime (struct employee *emp_ptr, int size)
{
// Calculate the overtime hours
if (emp_ptr->hours > STD_HOURS) {
emp_ptr->overtime = emp_ptr->hours - STD_HOURS;
}
else {
emp_ptr->overtime = 0;
}
}
// *******************************************************************
// Function calculate_gross_pay
//
// Purpose: This function will calculate the gross pay for a given
// number of employees.
//
// Parameters: employees - the array of structures for employees
// size - the number of employees to input
//
// Returns: Nothing (void)
//
// *******************************************************************
void calculate_gross_pay (struct employee *emp_ptr, int size)
{
// Calculate the gross pay if overtime was worked
if (emp_ptr->overtime > 0) {
emp_ptr->gross = STD_HOURS * emp_ptr->wage; // Calculates pay for standard hours
// Calculates pay for overtime hours and adds it to the total
emp_ptr->gross += emp_ptr->overtime * emp_ptr->wage * OVTP; }
else { // If no overtime was worked
emp_ptr->gross = emp_ptr->wage * emp_ptr->hours;
}
}
/************************************************************************/
/* Function: printData */
/* */
/* Purpose: Outputs to screen in a table format the following: */
/* - Employee First and Last Name */
/* - Employee clock number */
/* - Wage rate for an employee. */
/* - Total hours worked by employee. */
/* - Overtime Hours. */
/* - Gross Pay. */
/* */
/* Parameters: emp_ptr - pointer to array of structures */
/* size - number of employees to process */
/* */
/* Returns: Nothing, since emp_ptr is passed by reference */
/************************************************************************/
void printData ( struct employee * emp_ptr, int size )
{
int n; /* counter used in for loop to keep track of iterations */
/* prints the output for all employees to the screen */
for (n = 0; n < SIZE; n++)
{
printf ("%-20.20s %06i $%5.2f %4.1f %4.1f $%7.2f ",
emp_ptr->name,
emp_ptr->id,
emp_ptr->wage,
emp_ptr->hours,
emp_ptr->overtime,
emp_ptr->gross);
++emp_ptr; /* move to next employee */
}
printf (" ");
}
/************************************************************************/
/* Function: Main */
/************************************************************************/
int main()
{
/* A structure array to hold information on employees */
struct employee emp [SIZE] = { {"Connie Cobol", 98401, 10.60},
{"Frank Fortran", 526488, 9.75},
{"Mary Apl", 765349, 10.50},
{"Jeff Ada", 34645, 12.25},
{"Anton Pascal", 127615, 10.0}
};
int i; /* loop and array index */
struct employee * emp_ptr; /* pointer to an employee structure */
emp_ptr = emp; /* point to beginning of emp array */
// Function declarations
void get_hours (struct employee *emp_ptr, int size);
void calculate_gross_pay (struct employee *emp_ptr, int size);
void calculate_overtime (struct employee *emp_ptr, int size);
void print_employee_wages (struct employee *emp_ptr, size);
/* Read in hours, and calculate overtime and gross - Call by Value */
for (i=0; i < SIZE; ++i)
{
/* Get user input for the hours worked for each employee */
emp_ptr->hours = getHours ( emp , SIZE );
/* Add function calls to calculate employee overtime and gross */
emp_ptr->overtime = calculate_overtime (emp , SIZE);
emp_ptr->gross = calculate_gross_pay (emp , SIZE);
++emp_ptr; /* point to next employee */
} /* end for */
/* Print column headers for employees */
/* Print employee data to the screen - Call by Reference */
printData ( emp, SIZE );
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
// CONSTANTS
#define STD_HOURS 40.0f // Standard number of work hours per week
#define OVERTIME_MULTIPLIER 1.5f // Overtime wage calculator
#define NUM_EMPLOYEES 5 // The program will ask for 5 entries, store 5 values in each array etc.
#define MAX_HOURS 60 // Does not allow an employee to work more than 60 hours in a week
#define MAX_NAME_LEN 20 // Maximum name constraint. Can now be changed here at will
struct employee
{
char name [MAX_NAME_LEN];
long id_number;
float wage;
float hours;
float overtime;
float gross;
};
int main (void)
{
// Initialize an array of structures with the employee number and wage rate
struct employee employees[NUM_EMPLOYEES] =
{
{"Connie Cobol", 98401, 10.60}, {"Mary Apl", 526488, 9.75}, {"Frank Fortran", 765349, 10.50},
{"Jeff Ada", 34645, 12.25}, {"Anton Pascal", 127615, 10.00}
};
// Function declarations
void get_hours (struct employee *emp_ptr, int size);
void calculate_gross_pay (struct employee *emp_ptr, int size);
void calculate_overtime (struct employee *emp_ptr, int size);
void print_employee_wages (struct employee *emp_ptr, int size);
//-*-*-*-*-Begin creating the nuts and bolts-*-*-*-*-
get_hours(employees, NUM_EMPLOYEES);
calculate_overtime(employees, NUM_EMPLOYEES);
calculate_gross_pay(employees, NUM_EMPLOYEES);
print_employee_wages(employees, NUM_EMPLOYEES);
}
// *******************************************************************
// Function get_hours
//
// Purpose: This function will get the hours worked for a given
// number of employees. It will save each value into an
// array.
//
// Parameters: employees - the array of structures for employees
// size - the number of employees to input
//
// Returns: Nothing (void)
//
// *******************************************************************
void get_hours (struct employee *emp_ptr, int size)
{
int i; // To increment the loop
for (i = 0; i < size; ++emp_ptr, ++i) {
// Request the number of hours worked for each employee
printf("Please enter the number of hours worked for employee %s: ", emp_ptr->name);
scanf("%f", &emp_ptr->hours);
// Make sure the hours entered is in a proper range
while (emp_ptr->hours < 0 || emp_ptr->hours > MAX_HOURS) {
printf("*That is not a valid number of hours.* Please re-enter the number of hours worked for employee %s: ", emp_ptr->name); scanf("%f", &emp_ptr->hours);
}
}
}
// *******************************************************************
// Function calculate_overtime
//
// Purpose: This function will calculate the number of overtime
// hours worked by a given number of employees. It will
// save each value into an array.
//
// Parameters: employees - the array of structures for employees
// size - the number of employees to input
//
// Returns: Nothing (void)
//
// *******************************************************************
void calculate_overtime (struct employee *emp_ptr, int size)
{
int i; // To increment the loop
for (i = 0; i < size; ++emp_ptr, ++i) {
// Calculate the overtime hours if an employee worked over the standard amount
if (emp_ptr->hours > STD_HOURS) {
emp_ptr->overtime = emp_ptr->hours - STD_HOURS;
}
else {
// If the employee didn't work any overtime hours 0 is set as the value
emp_ptr->overtime = 0;
}
}
}
// *******************************************************************
// Function calculate_gross_pay
//
// Purpose: This function will calculate the gross pay for a given
// number of employees.
//
// Parameters: employees - the array of structures for employees
// size - the number of employees to input
//
// Returns: Nothing (void)
//
// *******************************************************************
void calculate_gross_pay (struct employee *emp_ptr, int size)
{
int i; // To increment the loop
for (i = 0; i < size; ++emp_ptr, ++i) {
// Calculate the gross pay if overtime was worked
if (emp_ptr->overtime > 0) {
emp_ptr->gross = STD_HOURS * emp_ptr->wage; // Calculates pay for standard hours
// Calculates pay for overtime hours and adds it to the total
emp_ptr->gross += emp_ptr->overtime * emp_ptr->wage * OVERTIME_MULTIPLIER; }
else { // If no overtime was worked
emp_ptr->gross = emp_ptr->wage * emp_ptr->hours;
}
}
}
// *******************************************************************
// Function print_employee_wages
//
// Purpose: This function will print out the gross
// wages of each employee.
//
// Parameters: employees - the array of structures for employees
// size - the number of employees to input
//
// Returns: Nothing (void)
//
// *******************************************************************
void print_employee_wages (struct employee *emp_ptr, int size)
{
int i; // To increment the loop
// Declare functions called
void get_totals (struct employee *emp_ptr, float total_array[], int size);
void get_minimum (struct employee *emp_ptr, float minimum[], int size);
void get_maximum (struct employee *emp_ptr, float maximum[], int size);
// Print out header information for data to be displayed
printf (" -------------------------------------------------------------- ");
printf ("Name Clock# Wage Hours OT Gross ");
printf ("-------------------------------------------------------------- ");
// Print out employee information to the screen
for (i = 0; i < size; ++emp_ptr, ++i) {
printf ("%s %06li %.2f %.1f %.1f %4.2f ", emp_ptr->name ,emp_ptr->id_number, emp_ptr->wage, emp_ptr->hours, emp_ptr->overtime, emp_ptr->gross);
}
printf(" ");
}
sample output
Please enter the number of hours worked for employee Connie Cobol: 40
Please enter the number of hours worked for employee Mary Apl: 44
Please enter the number of hours worked for employee Frank Fortran: 45
Please enter the number of hours worked for employee Jeff Ada: 50
Please enter the number of hours worked for employee Anton Pascal: 51
--------------------------------------------------------------
Name Clock# Wage Hours OT Gross
--------------------------------------------------------------
Connie Cobol 098401 10.60 40.0 0.0 424.00
Mary Apl 526488 9.75 44.0 4.0 448.50
Frank Fortran 765349 10.50 45.0 5.0 498.75
Jeff Ada 034645 12.25 50.0 10.0 673.75
Anton Pascal 127615 10.00 51.0 11.0 565.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.