Need it to work in Ideone

Oct 7th, 2014
Price: $5 USD

Question description


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;
          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.

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, ot, 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.


Assignment 5 Templates

Here are some templates you could use for this week's assignment. There are many ways to do it, below are just some ideas  you are welcome to use for your design. The template below provides a basic set up that you can modify and includes a sample function to print the array of structures ... which could prove useful as you add new functions and debug your code. It should compile and run in whatever compiler you use (including IDEOne). 

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 members inside that structure.  Additionally, because it is an Array of Structures, you will also have the added bonus of being able to access any array element as well.  Remember that in homework 4 (functions) you had to pass one or more arrays as needed to your functions, which 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:  

** 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 <stdio.h>

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

/* 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 [ ], int size);

** 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 */

  /* printf 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 ()
  /* Variable Declaration and initialization */
 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 reading, calculating, and printing as needed */

 /* Function call to output results to the screen in table format. */
 Output_results_screen (employeeData, NUM_EMPL);

 return(0); /* success */

} /* main */

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.  

Another way to do this 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 hours for an employee to a function and returning back the overtime hours, and having this 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 each function to have multiple parameters (for example, to figure out gross pay, you'll need to pass at least wage, hours, and overtime).

/* In your main function, this is how you could call your needed       */
/* functions using this method.   You'll need to define the            */
/* employeeData, which I declared and intialized in the other example. */

int main ()

 for (i = 0; i < SIZE; ++i)

 /* call functions to process each employee, one at a time */ 
 employeeData [ i ].hours = Get_Hour ( employeeData[i].clock );
 employeeData [ i ].overtime = Calc_Overtime ( employeeData[i].hours );
 employeeData [ i ].gross = Calc_Gross ( employeeData[i].wage, employeeData[i].hours,
                                                    employeeData[i].overtime, employeeData[i].gross ):
 } /* for */

} /* main */

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.

Tutor Answer

(Top Tutor) Alonso L
School: Cornell University

Studypool has helped 1,244,100 students

Review from student
" Top quality work from this guy! I'll be back! "
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1823 tutors are online

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors