Assignment title: Information
HOMEWORK 5
STRUCTURES
Write a C program that will calculate the gross pay of a set of employees.
The program should prompt the user to enter the number of hours each employee worked. When prompted, key in the hours shown below.
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.
--------------------------------------------------------------------------
Clock# Wage Hours OT Gross
--------------------------------------------------------------------------
098401 10.60 51.0 11.0 598.90
526488 9.75 42.5 2.5 426.56
765349 10.50 37.0 0.0 388.50
034645 12.25 45.0 5.0 581.88
127615 8.35 0.0 0.0 0.00
You should implement this program using the following structure to store the information for each employee.
/* This is the structure you will need, feel free to modify as needed */
struct employee
{
long int id_number; /* or just int id_number; */
float wage;
float hours;
float overtime;
float gross;
};
In your main function, define an array of structures, and feel free to initialize the clock and wage values in your array declaration. You must prompt for each employee's hours and calculate their corresponding overtime and gross pay.
Use the following information to initialize your data.
98401 10.60
526488 9.75
765349 10.50
34645 12.25
127615 8.35
Create an array of structures with 5 elements, each being of type struct employee. Initialize the array with the data provided and reference the elements of the array with the appropriate subscripts. Like the previous homework, use multiple functions in your answer and continue to use constants as needed. The only array you need is the array of structures, you don't need to create a separate array for clock, wage, hours, overtime, and gross. You can either pass the entire array of structures to each function, or pass values that work on one array element (and its associated structure members) at a time ... either will work.
Templates have been provided this week if you would like to use them. As always, the templates are simply suggestions to help you get started. Feel free to improvise and seek out your own design and solutions as long as they meet the requirements specified in the assignment. Whatever way you do it, don't use global variables ... I don't want all your functions looking like: void foo () with no parameters passed or used. There are benefits and drawbacks to each template approach. Good luck with these templates, or feel free to create your own from scratch.
Optional Challenge
For those of you more experienced programmers, continue with the challenges below that were offered in last week's assignment. The additional challenge this week would be referencing the information from structures and/or array of structures, as well as continuing to encapsulate the logic into functions that can be called to do this work.
• Calculate and print the total sum of Wage, Hours, Overtime, and Gross values
• Calculate and print the average of the Wage, Hours, Overtime, and Gross Values
Clock# Wage# Hours OT Gross
----- ----- ----- ---- -------
098401 10.60 51.0 11.0 598.90
526488 9.75 42.5 2.5 426.56
765349 10.50 37.0 0.0 388.50
034645 12.25 45.0 5.0 581.88
127615 8.35 0.0 0.0 0.00
----- ----- ----- ---- -------
Total 51.45 175.5 18.5 1995.84
Average 10.29 35.1 3.7 399.17
Assignment 5 Code Templates
I've provided two templates that you are welcome to use for this week's assignment. One is passing the address of a single array of structures along with the array size to each function to peform a specific set of tasks. This is much easier than last week in that you only need to pass one array, an array of structures, rather than figuring out which set of arrays you need to pass to each function.
An alternate approach is to pass individual structure member values within each array element of your array of structures. In this case, you pass the values you need to each function and have that function return a result, such as a the gross pay or overtime of a particular employee.
Whatever way you do it, don't use global variables ... I don't want all your functions looking like: void foo ( ) with no parameters passed or used. There are benefits and drawbacks to each approach. Good luck with these templates, or feel free to create your own from scratch. Of course, if you have lots of time on your hands, try them both :)
Option 1: Call by Reference Code Template
There are many ways to do the assignment, below are just some ideas you are welcome to use for your design. The template below provides you with a basic starting point that you can modify, and includes a complete function to print all the array elements and members in the array of structures ... which could prove useful as you add new functions and debug your code. The template should compile and run in whatever compiler you use (including IDEOne).
I always found that creating a function to display the data first is a good initial step. Then you can call it any time to check that your variables are initializing and updating correctly. Don't feel you have to create all your functions right way. Develop and test the ones you need one at a time. This Divide and Conquer approach will serve you well and you won't be worried about initially debugging lots of errors.
One thing you'll notice if you decide to do it this way, is that you can just pass a single array of structures to any function, and you will have access to any combination of the array elements (in this case, each employee) along with members inside that structure. This is an example of Call by Reference (i.e., address). Remember that in homework 4 (functions) you had to pass one or more arrays as needed to your functions. This can can be quite a pain if you had lots of different attribute information about each employee (for example, if in the future I added their name, hire date, salary grade, ...).
Review the template below and feel free to access it at: http://ideone.com/wjrONU
/*********************************************************************
**
** HOMEWORK: #5 Structures
**
** Name: [Enter your Name]
**
** Class: C Programming
**
** Date: [enter the date]
**
** Description: This program prompts the user for the number of hours
** worked for each employee. It then calculates gross pay
** including overtime and displays the results in table. Functions
** and structures are used.
**
/**********************************************************************/
/*Define and Includes */
#include
/* Define Constants */
#define NUM_EMPL 5
/* Define a global structure to pass employee data between functions */
/* Note that the structure type is global, but you don't want a variable */
/* of that type to be global. Best to declare a variable of that type */
/* in a function like main or another function and pass as needed. */
struct employee
{
long id_number;
float wage;
float hours;
float overtime;
float gross;
};
/* define prototypes here for each function except main */
void Output_Results_Screen (struct employee emp [ ], int size);
/* add your functions here */
/********************************************************************
** Function: Output_Results_Screen
**
** Purpose: Outputs to screen in a table format the following
** information about an employee: Clock, Wage,
** Hours, Overtime, and Gross Pay.
**
** Parameters:
**
** employeeData - an array of structures containing
** employee information
** size - number of employees to process
**
** Returns: Nothing (void)
**
********************************************************************/
void Output_Results_Screen ( struct employee employeeData[], int size )
{
int i; /* loop index */
/* print information about each employee */
for (i = 0; i < size ; ++i)
{
printf(" %06li %5.2f %4.1f %4.1f %8.2f \n",
employeeData[i].id_number, employeeData[i].wage, employeeData[i].hours,
employeeData[i].overtime, employeeData[i].gross);
} /* for */
} /* Output_results_screen */
int main ()
{
/* Set up a local variable and initialize the clock and wages of my employees */
struct employee employeeData[NUM_EMPL] = {
{ 98401, 10.60 },
{ 526488, 9.75 },
{ 765349, 10.50 },
{ 34645, 12.25 },
{ 127615, 8.35 }
};
/* Call various functions needed to read and calculate info */
/* Print the column headers */
/* Function call to output results to the screen in table format. */
Output_Results_Screen (employeeData, NUM_EMPL);
return(0); /* success */
} /* main */
Option 2: Combination of Call by Value and Call by Reference Code Template
Another way to do this assignment is to pass member values of specific elements into the array of structures from a function like main to other functions that will return a value in the result. For example, passing the unique clock number for an employee to a function and returning back the hours they worked in a week, and having this function call in a loop that will process each employee. If you do it this way, you have to pass the right information about the employee as needed by each function, i.e., expect some functions to have multiple parameters (for example, to figure out gross pay, you'll need to pass in at least wage, hours, and overtime).
Review the template below and feel free to access it at: http://ideone.com/XXyaJ6
/**********************************************************************
**
** HOMEWORK: #5 Structures
**
** Name: [Enter your Name]
**
** Class: C Programming
**
** Date: [enter the date]
**
** Description: This program prompts the user for the number of hours
** worked for each employee. It then calculates gross pay
** including overtime and displays the results in table. Functions
** and structures are used.
**
/**********************************************************************/
/*Define and Includes */
#include
/* Define Constants */
#define NUM_EMPL 5
/* Define a global structure to pass employee data between functions */
/* Note that the structure type is global, but you don't want a variable */
/* of that type to be global. Best to declare a variable of that type */
/* in a function like main or another function and pass as needed. */
struct employee
{
long int id_number;
float wage;
float hours;
float overtime;
float gross;
};
/* define prototypes here for each function except main */
float Get_Hours (long int id_number);
void Output_Results_Screen (struct employee emp [ ], int size);
/* Add your functions here */
/* Add Function Comment Header for Get_Hours */
float Get_Hours (long int id_number)
{
/* Add code as needed ... this is an example of a stub */
}
/*******************************************************************
** Function: Output_Results_Screen
**
** Purpose: Outputs to screen in a table format the following
** information about an employee: Clock, Wage,
** Hours, Overtime, and Gross Pay.
**
** Parameters:
**
** employeeData - an array of structures containing
** employee information
** size - number of employees to process
**
** Returns: Nothing (void)
**
*******************************************************************/
void Output_Results_Screen ( struct employee employeeData[], int size )
{
int i; /* loop index */
/* print information about each employee */
for (i = 0; i < size ; ++i)
{
printf(" %06li %5.2f %4.1f %4.1f %8.2f \n",
employeeData[i].id_number, employeeData[i].wage, employeeData[i].hours,
employeeData[i].overtime, employeeData[i].gross);
} /* for */
} /* Output_results_screen */
int main ()
{
/* Set up a local variable to store the employee information */
struct employee employeeData[NUM_EMPL] = {
{ 98401, 10.60 },
{ 526488, 9.75 },
{ 765349, 10.50 }, /* initialize clock and wage values */
{ 34645, 12.25 },
{ 127615, 8.35 }
};
int i; /* loop and array index */
/* Call functions as needed to read and calculate information */
for (i = 0; i < NUM_EMPL; ++i)
{
/* Prompt for the number of hours worked by the employee */
employeeData[i].hours = Get_Hours (employeeData[i].id_number);
/* Add other function calls as needed to calculate overtime and gross */
} /* for */
/* Print the column headers */
/* Function call to output results to the screen in table format. */
Output_Results_Screen (employeeData, NUM_EMPL);
return(0); /* success */
} /* main */