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: 3678054 • 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

Please use this template so I can compare with my code that isn't working.

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(" ");
}


output


Please enter the number of hours worked for employee Connie Cobol: 8                                                                                        
Please enter the number of hours worked for employee Mary Apl: 2                                                                                            
Please enter the number of hours worked for employee Frank Fortran: 3                                                                                       
Please enter the number of hours worked for employee Jeff Ada: 4                                                                                            
Please enter the number of hours worked for employee Anton Pascal: 9                                                                                        
                                                                                                                                                            
--------------------------------------------------------------                                                                                              
Name                    Clock# Wage    Hours   OT      Gross                                                                                               
--------------------------------------------------------------                                                                                              
Connie Cobol            098401 10.60   8.0     0.0     84.80                                                                                               
Mary Apl                526488 9.75    2.0     0.0     19.50                                                                                               
Frank Fortran           765349 10.50   3.0     0.0     31.50                                                                                               
Jeff Ada                034645 12.25   4.0     0.0     49.00                                                                                               
Anton Pascal            127615 10.00   9.0     0.0     90.00                                                                                               

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