Write a C program that will calculate the gross pay of a set of employees. Decla
ID: 3678053 • 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. 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. --------------------------------------------------------- Name Clock# Wage Hours OT Gross --------------------------------------------------------- Connie Cobol 098401 10.60 51.0 11.0 598.90 Mary Apl 526488 9.75 42.5 2.5 426.56 Frank Fortran 765349 10.50 37.0 0.0 388.50 Jeff Ada 034645 12.25 45.0 5.0 581.88 Anton Pascal 127615 10.00 40.0 0.0 400.00 --------------------------------------------------------- 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; 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 Initialize the array with the data provided. 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. Add code needed to generate a total and average of the hour, overtime, and gross pay values, just like you did in the previous homework. Please use template so I can understand what I'm doing wrong with my thinking in writing code. Thanks Here is a template to use for homework 7. If you want to deviate from it, that is fine, as long as you follow the rules mentioned above. The template will compile and start printing values right out of the box. It is your job to fill in the missing code and provide input so that it displays the correct information. /************************************************************************/ /* */ /* HOMEWORK: 7 */ /* */ /* Name: Joe Student */ /* */ /* Class: C Programming, Cybercourse */ /* */ /* Date: 6/29/2012 */ /* */ /* 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 STD_HOURS 40.0 #define TIME_HALF 1.5 #define SIZE 5 /* 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 */ }; /************************************************************************/ /* Function: getHours */ /* */ /* Purpose: Obtains the number of hours worked per employee from */ /* user and stores the results in an array that is */ /* passed back to the calling program by reference. */ /* */ /* Parameters: emp_ptr - pointer to array of structures */ /* size - number of employees to process */ /* */ /* Returns: Nothing, since emp_ptr is passed by reference */ /************************************************************************/ void getHours ( struct employee * emp_ptr, int size ) { } /************************************************************************/ /* Function: calcOvertime */ /* */ /* Purpose: Calculates the number of overtime hours given the number*/ /* of hours worked. Overtime hours are the number of hours */ /* worked after 40 hrs. */ /* */ /* Parameters: emp_ptr - pointer to array of structures */ /* size - number of employees to process */ /* */ /* Returns: Nothing, since emp_ptr is passed by reference */ /************************************************************************/ void calcOvertime ( struct employee * emp_ptr, int size ) { } /************************************************************************/ /* Function: calcGrossPay */ /* */ /* Purpose: Calculates gross pay including overtime given the number*/ /* of hours an employee works and wage rate. */ /* */ /* Parameters: emp_ptr - pointer to array of structures */ /* size - number of employees to process */ /* */ /* Returns: Nothing, since emp_ptr is passed by reference */ /************************************************************************/ void calcGrossPay ( struct employee * emp_ptr, int size ) { } /************************************************************************/ /* Function: printTable */ /* */ /* 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 printTable ( struct employee * emp_ptr, int size ) { int n; /* counter used in for loop to keep track of iterations */ /* prints the table header to the screen */ printf (" ------------------------------------------------------------------- "); printf (" Name Clock# Wage Hours OT Gross "); printf ("--------------------------------------------------------------------- "); /* 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} }; /* Get user input for the hours worked for each employee */ getHours ( emp, SIZE ); /* Calculate gross pay for each employee */ calcOvertime ( emp, SIZE ); /* Calculate gross pay for each employee */ calcGrossPay ( emp, SIZE ); /* Print results to the screen */ printTable ( emp, SIZE ); return 0; }Explanation / Answer
Solution: See the code below:
----------------------------------------------------------
/************************************************************************//*
* *//* HOMEWORK: 7 *//* *//* Name: Joe Student *//*
* *//* Class: C Programming, Cybercourse *//*
* *//* Date: 6/29/2012 *//*
* *//* 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 <stdio.h>
#include <stdlib.h>
/* define all constants here */
#define STD_HOURS 40.0
#define TIME_HALF 1.5
#define SIZE 5
/* 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 */
};
/************************************************************************/
/* Function: getHours *//*
* *//* Purpose: Obtains the number of hours worked per employee from */
/* user and stores the results in an array that is */
/* passed back to the calling program by reference. */
/* *//* Parameters: emp_ptr - pointer to array of structures */
/* size - number of employees to process *//* */
/* Returns: Nothing, since emp_ptr is passed by reference */
/************************************************************************/
void getHours(struct employee * emp_ptr, int size) {
int n;/* counter used in for loop to keep track of iterations */
/* gets hours worked for each employee */
for (n = 0; n < SIZE; n++) {
printf("Enter hours worked for %-20.20s %06i: ", emp_ptr->name,
emp_ptr->id);
scanf("%f", &emp_ptr->hours);
++emp_ptr; /* move to next employee */
}
}
/************************************************************************/
/* Function: calcOvertime *//* */
/* Purpose: Calculates the number of overtime hours given the number*/
/* of hours worked. Overtime hours are the number of hours */
/* worked after 40 hrs. *//* */
/* Parameters: emp_ptr - pointer to array of structures */
/* size - number of employees to process *//* */
/* Returns: Nothing, since emp_ptr is passed by reference */
/************************************************************************/
void calcOvertime(struct employee * emp_ptr, int size) {
int n;/* counter used in for loop to keep track of iterations */
/* calculates OT for each employee */
for (n = 0; n < SIZE; n++) {
emp_ptr->overtime = emp_ptr->hours - 40.0;
++emp_ptr; /* move to next employee */
}
}
/************************************************************************/
/* Function: calcGrossPay *//* */
/* Purpose: Calculates gross pay including overtime given the number*/
/* of hours an employee works and wage rate. *//* */
/* Parameters: emp_ptr - pointer to array of structures */
/* size - number of employees to process *//* */
/* Returns: Nothing, since emp_ptr is passed by reference */
/************************************************************************/
void calcGrossPay(struct employee * emp_ptr, int size) {
int n;/* counter used in for loop to keep track of iterations */
/* calculates gross pay for each employee */
for (n = 0; n < SIZE; n++) {
emp_ptr->gross = emp_ptr->hours * emp_ptr->wage;
++emp_ptr; /* move to next employee */
}
}
/************************************************************************/
/* Function: printTable *//* */
/* 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 printTable(struct employee * emp_ptr, int size) {
int n; /* counter used in for loop to keep track of iterations */
/* prints the table header to the screen */
printf(
" ------------------------------------------------------------------- ");
printf(" Name Clock# Wage Hours OT Gross ");
printf(
"--------------------------------------------------------------------- ");
/* 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 } };
/* Get user input for the hours worked for each employee */
getHours(emp, SIZE);
/* Calculate gross pay for each employee */
calcOvertime(emp, SIZE);
/* Calculate gross pay for each employee */
calcGrossPay(emp, SIZE);
/* Print results to the screen */
printTable(emp, SIZE);
return 0;
}
-------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.