////////////////////////////////////////////////////////////////////////////////
//
// Homework: 7
//
// Name: Keith Rowe
//
// Class: C Programming, Fall 2016
//
// Date: 10/18/2016
//
// Description: Program that uses structures to calculate employee pay. The only
// user input is hours worked for that particular week. All other data is stored
// in structures and calculated in subsequent functions. After the user enters
// hours worked the program will use pointers to calculate the following:
// overtime hours, gross pay, weekly totals,and weekly averages. Final output
// is a table that is easy for the user to read.
//
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// constants
#define NUM_EMPL 5
#define OT_FACTOR 1.5f
#define STD_HOURS 40.0f
// global structures
struct employee // basic employee data
{
char empName[20];
long int clockNumber;
float wageRate;
float hours;
float gross;
float overtime;
};
struct report // reports for management - a way to get a snapshot
{
//totals
float totalWage;
float totalHours;
float totalGross;
float totalOvertime;
//averages
float avgWage;
float avgHours;
float avgGross;
float avgOvertime;
//minimums
float minWage;
float minHours;
float minGross;
float minOvertime;
//maximums
float maxWage;
float maxHours;
float maxGross;
float maxOvertime;
};
// function prototypes
void getHours (struct employee * empPtr, int size);
void calcOvertime (struct employee *empPtr, int size);
void calcGross (struct employee * empPtr, int size);
void printData (struct employee * empPtr, struct report weeklyReport[], int size);
void empTotals (struct employee * empPtr, struct report weeklyReport[], int size);
void empAverages (struct report * reportPtr, int size);
void empMinimums (struct employee *empPtr, struct report weeklyReport[], int size);
void empMaximums (struct employee * empPtr, struct report weeklyReport[], int size);
int main()
{
/* Variable Declarations */
struct employee employeeData[NUM_EMPL]=
{ // initialize clock and wage values
{"Connie Coblol", 98401, 10.60},
{"Mary Apl", 526488, 9.75 },
{"Frank Fortran", 765349, 10.50 },
{"Jeff Ada", 34645, 12.25 },
{"Anton Pascal", 127615, 10.00 }
}; // end employeeData
struct report weeklyReport[]={0}; //end weeklyReport
struct employee *empPtr; //pointer used to reference employee data structure
struct report *reportPtr; //pointer to reference weekly report sructure
empPtr = employeeData; //set pointer to first element of employeeData
reportPtr = weeklyReport; //set point to first element of weeklyReport
// Function call to get hours worked for each employee
getHours (employeeData, NUM_EMPL);
// Function call to calculate overtime
calcOvertime (employeeData, NUM_EMPL);
// Function call to calculate gross pay
calcGross (employeeData, NUM_EMPL);
// Function call to calculate totals
empTotals(employeeData, weeklyReport, NUM_EMPL);
// Function to calculate averages
empAverages (weeklyReport, NUM_EMPL);
// Function to calculate minimums
empMinimums(employeeData, weeklyReport, NUM_EMPL);
// Function to calculate maximums
empMaximums(employeeData, weeklyReport, NUM_EMPL);
// Function call to output results to the screen
printData (employeeData, weeklyReport, NUM_EMPL);
return (0);
}// end main
////////////////////////////////////////////////////////////////////////////////
// Function: getHours
//
// Purpose: Obtains input from user, the number of hours worked per employee
// and stores the results in an array that is passed back to the calling
// function by reference.
//
// Parameters
// employeeData - structure that contains employee data such as wage, clock
// number, and stores hours worked, overtime, and gross for the week.
// size - Number of employees to process
//
// Returns: Nothing
//
////////////////////////////////////////////////////////////////////////////////
void getHours (struct employee *empPtr, int size)
{
int idx; // loop counter
//Get Hours for each employee
for (idx = 0; idx < size; ++idx)
{
printf("\nEnter hours worked by emp # %06li: ", empPtr -> clockNumber);
scanf ("%f", &empPtr -> hours);
while (empPtr -> hours < 0)
{
printf("\n!!! Hours must be positive numbers !!!\n");
printf ("Enter the number of hours worked by employee # %06li: ", empPtr -> clockNumber);
scanf("%f", &empPtr -> hours);
} //end while
++ empPtr; // increment employee pointer
} // end for
printf("\n\n");
}// end getHours
////////////////////////////////////////////////////////////////////////////////
// Function: calcOvertime
//
// Purpose: Calculates overtime hours for an employee based on input from other
// functions. calcOvertime calculates overtime by pulling data from the array
// hours. It then populates the array overtime with overtime hours.
// Data is passed back to the calling function by reference.
//
// Parameters
// employeeData - structure that contains employee data such as wage, clock
// number, and stores hours worked, overtime, and gross for the week.
// size: number of employees to process.
//
// Return: nothing
//
////////////////////////////////////////////////////////////////////////////////
void calcOvertime (struct employee * empPtr, int size)
{
int idx; // index to count loop
for (idx = 0; idx < size; ++idx)
{
if (empPtr -> hours > STD_HOURS)
{
empPtr -> overtime = (empPtr -> hours - STD_HOURS); //calculate OT
} //end if
else
{
empPtr -> overtime = 0;
} // end else
++empPtr; //increment employee pointer
} // end for
}// end calcOvertime
////////////////////////////////////////////////////////////////////////////////
// Function: calcGross
//
// Purpose: Calculates gross pay for an employee based on input from other
// functions. Calculates overtime by pulling data from the arrays
// wageRate, overtime, and hours. It then populates the array gross with gross
// pay. Data is passed back to the calling function by reference.
//
// Parameters
// employeeData - structure that contains employee data such as wage, clock
// number, and stores hours worked, overtime, and gross for the week.
// size: number of employees to process.
//
// Returns: nothing
//
////////////////////////////////////////////////////////////////////////////////
void calcGross (struct employee *empPtr, int size)
{
int idx; // index to count loop
for (idx = 0; idx < size; ++idx)
{
empPtr -> gross = (empPtr ->wageRate * (empPtr -> hours - empPtr -> overtime)) + (empPtr -> wageRate * empPtr -> overtime * OT_FACTOR); //calculate weekly wage
++ empPtr; //increment employee pointer
}// end for
}// end calcGross
////////////////////////////////////////////////////////////////////////////////
// Function: printData
//
// Purpose: Function to print out various arrays that contain employee data. It
// prints out a header with titles, employee data in rows and trailing data.
//
// Parameters
// employeeData - structure that contains employee data such as wage, clock
// number, and stores hours worked, overtime, and gross for the week.
// weeklyReport - structure that contains weekly totals and averages for wage
// hours worked, overtime hours, and gross pay
// size: number of employees to process.
//
// Returns: nothing
//
////////////////////////////////////////////////////////////////////////////////
void printData (struct employee *empPtr, struct report weeklyReport[], int size)
{
int idx; //loop counter
printf ("\t----------------------------------------------------------\n");
printf ("\tName Clock# Wage Hours OT Gross\n");
printf ("\t----------------------------------------------------------\n");
for (idx = 0; idx<size; ++idx)
{
printf ("\t%-18.18s %06li %7.2f %6.1f %7.1f %9.2f\n", empPtr -> empName, empPtr -> clockNumber, empPtr -> wageRate, empPtr -> hours, empPtr -> overtime, empPtr ->gross);
++empPtr;// increment employee pointer
}// end for
printf ("\t----------------------------------------------------------\n");
printf ("\tTotal\t\t\t\t\t %7.2f %6.1f %7.1f %9.2f\n", weeklyReport->totalWage, weeklyReport->totalHours, weeklyReport->totalOvertime, weeklyReport->totalGross);
printf ("\tAverage\t\t\t\t\t %5.2f %6.1f %7.1f %9.2f\n", weeklyReport->avgWage, weeklyReport->avgHours, weeklyReport->avgOvertime, weeklyReport->avgGross);
printf ("\tMinimum\t\t\t\t\t %5.2f %6.1f %7.1f %9.2f\n", weeklyReport->minWage, weeklyReport->minHours, weeklyReport->minOvertime, weeklyReport->minGross);
printf ("\tMaximum\t\t\t\t\t %5.2f %6.1f %7.1f %9.2f\n", weeklyReport->maxWage, weeklyReport->maxHours, weeklyReport->maxOvertime, weeklyReport->maxGross);
printf("\n\n");
printf("\n\n");
}// end printData
////////////////////////////////////////////////////////////////////////////////
// Function: empTotals
//
// Purpose: Function to calculate totals for the. It will find the sum of
// various arrays then populate the array empTotals.
//
// Parameters
// employeeData - structure that contains employee data such as wage, clock
// number, and stores hours worked, overtime, and gross for the week.
// weeklyReport - structure that contains weekly totals and averages for wage
// hours worked, overtime hours, and gross pay
// size: number of employees to process.
//
// Return: nothing
////////////////////////////////////////////////////////////////////////////////
void empTotals (struct employee *empPtr, struct report weeklyReport[], int size)
{
int idx; //loop counter
for (idx = 1; idx< size; ++idx)
{
weeklyReport->totalWage += empPtr -> wageRate;
weeklyReport->totalHours += empPtr -> hours;
weeklyReport->totalOvertime += empPtr -> overtime;
weeklyReport->totalGross += empPtr -> gross;
++empPtr; // increment employee pointer
}// end for
}// end empTotals
////////////////////////////////////////////////////////////////////////////////
// Function: empAverages
//
// Purpose: Calculate weekly averages for employees based on data stored in the
// array totals. Function requres no input from the user.
//
// Parameters:
// weeklyReport - structure that contains weekly totals and averages for wage
// hours worked, overtime hours, and gross pay
// size: number of employees to process.
//
// Return: nothing
////////////////////////////////////////////////////////////////////////////////
void empAverages (struct report *reportPtr, int size)
{
reportPtr -> avgWage = reportPtr -> totalWage / size;
reportPtr -> avgHours = reportPtr -> totalHours / size;
reportPtr -> avgOvertime = reportPtr -> totalOvertime / size;
reportPtr -> avgGross = reportPtr -> totalGross / size;
}// end empAverages
////////////////////////////////////////////////////////////////////////////////
// Function: empMinimums
//
// Purpose: Calculate the weekly minimum value for each element of the weekly
// report ie wage, hours, OT hours, and gross
//
// Parameters:
// employeeData - structure that contains employee data such as wage, clock
// number, and stores hours worked, overtime, and gross for the week.
// weeklyReport - structure that contains weekly totals and averages for wage
// hours worked, overtime hours, and gross pay
// size: number of employees to process.
//
// Return: nothing populates minimums array in weeklyReports
////////////////////////////////////////////////////////////////////////////////
void empMinimums (struct employee *empPtr, struct report weeklyReport[], int size)
{
int idx; //loop counter
weeklyReport->minWage = empPtr -> wageRate;
weeklyReport->minHours = empPtr -> hours;
weeklyReport->minOvertime = empPtr -> overtime;
weeklyReport->minGross = empPtr -> gross;
for (idx = 0; idx < size; ++idx)
{
if (weeklyReport->minWage > empPtr -> wageRate)
{
weeklyReport->minWage = empPtr -> wageRate;
}// end if
if (weeklyReport->minHours > empPtr -> hours)
{
weeklyReport->minHours = empPtr -> hours;
}// end if
if (weeklyReport->minOvertime > empPtr -> overtime)
{
weeklyReport->minOvertime = empPtr -> overtime;
}// end if
if (weeklyReport->minGross > empPtr -> gross)
{
weeklyReport->minGross = empPtr -> gross;
}// end if
++empPtr; //increment employee pointer
}// end for
}// end empMinimums
////////////////////////////////////////////////////////////////////////////////
// Function: empMaximums
//
// Purpose: Calculate the weekly maximum value for each element of the weekly
// report ie wage, hours, OT hours, and gross
//
// Parameters:
// employeeData - structure that contains employee data such as wage, clock
// number, and stores hours worked, overtime, and gross for the week.
// weeklyReport - structure that contains weekly totals and averages for wage
// hours worked, overtime hours, and gross pay
// size: number of employees to process.
//
// Return: nothing populates maximum array in weeklyReports
////////////////////////////////////////////////////////////////////////////////
void empMaximums (struct employee *empPtr, struct report weeklyReport[], int size)
{
int idx; //loop counter
weeklyReport->maxWage = empPtr -> wageRate;
weeklyReport->maxHours = empPtr -> hours;
weeklyReport->maxOvertime = empPtr -> overtime;
weeklyReport->maxGross = empPtr -> gross;
for (idx = 0; idx < size; ++idx)
{
if (weeklyReport->maxWage < empPtr -> wageRate)
{
weeklyReport->maxWage = empPtr -> wageRate;
}// end if
if (weeklyReport->maxHours < empPtr -> hours)
{
weeklyReport->maxHours = empPtr -> hours;
}// end if
if (weeklyReport->maxOvertime < empPtr -> overtime)
{
weeklyReport->maxOvertime = empPtr -> overtime;
}// end if
if (weeklyReport->maxGross < empPtr -> gross)
{
weeklyReport->maxGross = empPtr -> gross;
}// end if
++empPtr; //increment employee pointer
}// end for
}// end empMaximums