Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program that will calculate the gross pay of a set of employees. Decla

ID: 3678051 • 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. --------------------------------------------------------- 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; /* 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 please help using this template. Show below is an alternate code template that has a one loop in the main function instead of each called function. You can process each employee one at a time with this design. It passes and returns values, rather than passing by address to other functions. Saying that, in this template, the last function, printData, still passes the structure array by reference as it has a loop inside that function. Likewise from the original template, I have provided the printData function code if you wish to use it. The entire template below can also be accessed at: http://ideone.com/ug82Px /************************************************************************/ /* */ /* HOMEWORK: 7 */ /* */ /* Name: Joe Student */ /* */ /* Class: C Programming, Cybercourse */ /* */ /* Date: */ /* */ /* 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 /* 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 */ /* Remember to add function comment header block for each function */ /* like shown below for printData, a hint for getHours is below. */ float getHours ( int id ) { float hours; /* hours worked for the employee */ /* prompt with id to read in a value for hours */ return (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 */ /* 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_ptr->id ); /* Add function calls to calculate employee overtime and gross */ ++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>

/* Define Constants */
#define NUM_EMPL 5
#define OVERTIME_RATE 1.5f
#define STD_WORK_WEEK 40.0f

struct employee
{
    char first_name[20];
    char last_name[20];
    long clock_number;
    float wage_rate;
    float hours;
    float OT;
    float gross;
};

// struct to store the total/averate/minimum/maximum
struct statistic {
    float hours;
    float OT;
    float gross;
};

/* define prototypes here for each function except main */
void Get_Input (struct employee *e, int size);
void Gross_pay_calc (struct employee *e, int size);

void Output_results_screen (struct employee *e, int size,
    struct statistic *total, struct statistic *average, struct statistic *minumum, struct statistic *maximum);

void Total_and_Avg (struct employee *e, int size, struct statistic *total, struct statistic *average);
void Minimum_and_Maximum(struct employee *e, int size, struct statistic *total, struct statistic *average);


void Output_results_screen (struct employee *employeeData, int size,
    struct statistic *total, struct statistic *average, struct statistic *minimum, struct statistic *maximum)
{
    int idx; /* loop index */

    printf (" George Smith, C Programming, Fifth Homework Assignment ");
    printf (" ---------------------------------------------------------- ");
    printf (" Name            Clock# Wage    Hours    OT      Gross ");
    printf (" ---------------------------------------------------------- ");

/* printf information about each employee */
    for (idx = 0; idx < size ; ++idx, ++employeeData)
    {
        printf(" %s %s %06li %5.2f %4.1f %4.1f %6.2f ",employeeData->first_name,
            employeeData->last_name, employeeData->clock_number, employeeData->wage_rate,
            employeeData->hours,employeeData->OT, employeeData->gross);
    } /* for */

    printf (" ---------------------------------------------------------- ");
    printf (" Total: %5.1f %5.1f %5.2f ",total->hours, total->OT, total->gross);
    printf (" Average: %5.1f %5.1f %5.3f ", average->hours, average->OT, average->gross);
    printf (" Minimum: %5.1f %5.1f %5.2f ",minimum->hours, minimum->OT, minimum->gross);
    printf (" Maximum: %5.1f %5.1f %5.2f ",maximum->hours, maximum->OT, maximum->gross);
} /* Output_results_screen */

/*function for user input*/
void Get_Input (struct employee *employeeData, int size)
{
     int idx; /* loop index */

     /* printf information about each employee */
     for (idx = 0; idx < size ; ++idx, ++employeeData)
     {
         printf(" Enter hours worked for employee %06li : ",employeeData->clock_number);
         scanf("%f",&employeeData->hours);
     }
}
/*function to calculate overtime and gross pay*/
void Gross_pay_calc (struct employee *employeeData, int size)
{
     int idx; /* loop index */

     /* printf information about each employee */
     for (idx = 0; idx < size ; ++idx, ++employeeData)
     {
         if (employeeData->hours <= STD_WORK_WEEK)
         {
            employeeData->OT = 0.0;
            employeeData->gross = employeeData->wage_rate * employeeData->hours;
         }

         else if (employeeData->hours > STD_WORK_WEEK)
         {
              employeeData->OT = employeeData->hours - STD_WORK_WEEK;
              employeeData->gross = (STD_WORK_WEEK * employeeData->wage_rate) + (employeeData->OT * (OVERTIME_RATE * employeeData->wage_rate));
         }

     }
}

/*function to calculate total and average hours, overtime and gross pay*/
void Total_and_Avg (struct employee *employeeData, int size, struct statistic *total, struct statistic *average)
{
    int idx; /* loop index */
    float total_hours=0;
    float total_OT=0;
    float total_gross=0;
/* printf information about each employee */
     for (idx = 0; idx < size ; ++idx, ++employeeData)
    {
        total_hours+= employeeData->hours;
        total_OT+= employeeData->OT;
        total_gross+= employeeData->gross;
    }

    total->hours = total_hours;
    total->OT = total_OT;
    total->gross = total_gross;

    average->hours = total_hours / size;
    average->OT = total_OT / size;
    average->gross = total_gross / size;
}

/*function to calcuate minimum and maximum hours, OT and gross pay*/
void Minimum_and_Maximum(struct employee *employeeData, int size, struct statistic *minimum, struct statistic *maximum)
{

int idx;

// initialize the data
minimum->hours = -1;
minimum->OT = -1;
minimum->gross = -1;
maximum->hours = 0;
maximum->OT = 0;
maximum->gross = 0;

for (idx = 0; idx < size ; ++idx, ++employeeData)
{
    if (minimum->hours == -1) {
      minimum->hours = employeeData->hours;
      minimum->OT = employeeData->OT;
      minimum->gross = employeeData->gross;
    }

    // save the new smaller one
    if (minimum->hours > employeeData->hours) {
      minimum->hours = employeeData->hours;
    }
    if (minimum->OT > employeeData->OT) {
      minimum->OT = employeeData->OT;
    }
    if (minimum->gross > employeeData->gross) {
      minimum->gross = employeeData->gross;
    }

    // save the new larger one
    if (maximum->hours < employeeData->hours) {
      maximum->hours = employeeData->hours;
    }
    if (maximum->OT < employeeData->OT) {
      maximum->OT = employeeData->OT;
    }
    if (maximum->gross < employeeData->gross) {
      maximum->gross = employeeData->gross;
    }
}
}

int main()
{
    /* Variable Declaration and initialization */
    struct employee employeeData[NUM_EMPL] = {
    {"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}
};

struct statistic total, average, minimum, maximum;

/* Call various functions needed to reading, calculating, and printing as needed */
Get_Input(employeeData, NUM_EMPL);
Gross_pay_calc(employeeData, NUM_EMPL);
/* Function call to output results to the screen in table format. */
Total_and_Avg (employeeData, NUM_EMPL, &total, &average);
Minimum_and_Maximum(employeeData, NUM_EMPL, &minimum, &maximum);
Output_results_screen (employeeData, NUM_EMPL, &total, &average, &minimum, &maximum);
return(0); /* success */

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote