fork download
  1. #include <stdio.h>
  2.  
  3. #define SIZE 3
  4.  
  5. struct employee
  6. {
  7. int id;
  8. float wage;
  9. float hours;
  10. };
  11.  
  12. /* Function Prototypes */
  13. void getHours (struct employee * ptr, int size);
  14. void printData (struct employee * ptr, int size);
  15.  
  16. /*************************************************************
  17.  * Function - getHours
  18.  *
  19.  * Description: Will prompt for the number of hours worked
  20.  * for each employee stored in the employee
  21.  * array being passed
  22.  *
  23.  * Parameters: ptr - pointer to an array of employees
  24.  * size - the number of employees to process
  25.  *
  26.  * Returns: void (the pointer will update the passed array)
  27.  *
  28. ***************************************************************/
  29.  
  30. void getHours (struct employee * ptr, int size)
  31. {
  32. int i; /* loop index */
  33.  
  34. for (i = 0; i < size; ++i)
  35. {
  36. printf ("\nEnter hours for employee %06i: ", ptr->id);
  37. scanf ("%f", &ptr -> hours);
  38.  
  39. /* move pointer to next employee */
  40. ++ptr;
  41. }
  42. }
  43.  
  44. /*************************************************************
  45.  * Function - printData
  46.  *
  47.  * Description: For a given set of employees, this function
  48.  * will print a one line summary report on each
  49.  * employee. The total number of employees
  50.  * printed is based on the size parameter.
  51.  *
  52.  * Parameters: ptr - pointer to an array of employees
  53.  * size - the number of employees to process
  54.  *
  55.  * Returns: void (the pointer will update the passed array)
  56.  *
  57. ***************************************************************/
  58.  
  59. /* Function to print data about each employee */
  60. void printData (struct employee * ptr, int size)
  61. {
  62. int i; /* loop index */
  63.  
  64. printf ("\n\nID\tWage\tHours\n");
  65.  
  66. for (i = 0; i < size; ++i)
  67. {
  68. printf ("%06i\t%5.2f\t%5.1f\n", ptr->id, ptr->wage, ptr->hours);
  69.  
  70. /* move pointer to next employee */
  71. ++ptr;
  72. }
  73. }
  74.  
  75.  
  76. int main ()
  77. {
  78.  
  79. /* Array of structures to hold information on three employees */
  80. struct employee emp [SIZE] = { {98401, 10.60},
  81. {526488, 9.75},
  82. {765349, 10.50}
  83. };
  84.  
  85. struct employee * emp_ptr; /* a pointer that can point to variables of type structure employee */
  86.  
  87. emp_ptr = emp; /* set pointer to first element in the array, &emp [0] works as well */
  88.  
  89. /* Read in hours for each employee */
  90. getHours (emp, SIZE);
  91.  
  92. /* Print employee information to the screen */
  93. printData (emp, SIZE);
  94.  
  95. return (0);
  96.  
  97. }
Success #stdin #stdout 0s 5296KB
stdin
45.6
40.0
34.4
stdout
Enter hours for employee 098401: 
Enter hours for employee 526488: 
Enter hours for employee 765349: 

ID	Wage	Hours
098401	10.60	 45.6
526488	 9.75	 40.0
765349	10.50	 34.4