简体   繁体   中英

Calculations in 2nd and 3rd function not working properly

The problem lies in the 2nd and 3rd functions where 'gross_pay' array is called to function. I can't seem to obtain the 'gross pay' despite trying everything I can think of.

I thought I may have passed too many arrays to function but it appears like they're needed to complete my data. Everytime I edit my code it gets to the point of destroying all my progress. If I don't pass to function I'm able to complete all the calculations properly, but I'm required to introduce several functions into this code.

Any input or suggestions are appreciated.

#include <stdio.h>

/* constants */
#define STD_HOURS 40.0 /* hours per week */
#define SIZE 5 /* employees to process */
#define OT 1.5 /* for overtime calculation */
#define AVERAGE 5 /* division by 5 for obtaining averages */

/* function to obtain hours worked for employees */
void getHours(long int clock_id[], float hours_worked[]) {
  int count;
  for (count = 0; count < SIZE; count++) {
    printf("Enter the number of hours worked for employee #%li: ", clock_id[count]);
    scanf("%f", &hours_worked[count]);
  }
} /* end function */

/* function call to calculate overtime hours */
void overtime_grosspay(float overtime_hours[], float hours_worked[], float gross[]) {
  float hourly_Wage[0]; /* initialize array */
  int count;
  for (count = 0; count < SIZE; count++)
    if (hours_worked[count] > STD_HOURS) /* calculation for gross pay to include overtime */ {
      overtime_hours[count] = hours_worked[count] - STD_HOURS;
      gross[count] = hourly_Wage[count] * STD_HOURS + (hourly_Wage[count]
        * overtime_hours[count] * OT);
    }
} /* end function */

/* function call to calculate gross pay */
void regular_grosspay(float overtime_hours[], float hours_worked[], float gross[]) {
  float Hourly_wage[0]; /* initialize array */
  /*float Overtime_hours[0];  array for overtime pay */
  /*float gross_pay[0];   array for gross pay per employee */
  int count;
  for (count = 0; count < SIZE; count++)
    if (hours_worked[count] <= STD_HOURS) {
      overtime_hours[count] = 0;
      gross[count] = hours_worked[count] * Hourly_wage[count];
    }
} /* end function */

int main(void) {
  long int clock_number[SIZE] = {
    98401,
    526488,
    765349,
    34645,
    127615
  }; /* initialize array */
  float hourly_wage[SIZE] = {
    10.6,
    9.75,
    10.5,
    12.25,
    8.35
  }; /* initialize array */
  int count = 0; /* loop index */
  /* float d = 0.0; */
  float hours_worked[SIZE]; /* array for hours worked */
  float overtime_hours[SIZE]; /* array for overtime pay */
  float gross_pay[SIZE]; /* array for gross pay per employee */ #
#if 0
  float sum_of_wages = 0; /* total employee wages */
  float average_wage = 0; /* average of employee wages */
  float sum_of_hours = 0; /* total employee hours */
  float average_hours_worked = 0; /* average employee hours */
  float total_OT = 0; /* sum of employee overtime */
  float average_of_OT = 0; /* average of overtime */
  float total_gross = 0; /* sum of employee gross pay */
  float average_total_gross = 0; /* average of employee gross pay */ #
#endif

  /* Welcome message to user */
  printf("* * * Welcome to the Pay Calculator * * *\n\n");
  printf("This program calculates the gross pay for a set of 5 employees.\n\n");

  /* function to obtain employee hours */
  getHours(clock_number, hours_worked);

  /* function call to calculate overtime hours */
  overtime_grosspay(overtime_hours, hours_worked, gross_pay);

  /* function call to calculate gross pay without overtime */
  regular_grosspay(overtime_hours, hours_worked, gross_pay);

  {
    printf("\n\n-------------------------------------------------\n");
    printf("Clock#    Wage   Hours    OT    Gross\n");
    printf("-------------------------------------------------\n");
  }

  /* begin loop to display tablulated data */
  for (count = 0; count < SIZE; count++) {
    printf("%06li   %5.2f   %5.1f %5.1f  %7.2f\n", clock_number[count], hourly_wage[count], hours_worked[count], overtime_hours[count], gross_pay[count]);
  } /* end for loop */

  printf("\nThank you for using the Pay Calculator. Good bye!\n\n");

  return 0;
} /* end main */

This line:

float hourly_Wage[0];  /* initialize array */

creates an array of size 0 . You probably want:

float hourly_Wage[SIZE];  /* initialize array */

if you want to define it here. But then you don't have the values. Using your model for passing arrays in and out, you'll probably want to pass hourly_wage in when you call overtime_grosspay() I've fixed that my copy of the code, and it seems to work now.


Also a couple of typos, probably as you pasted into StackOverflow:

This line fails to close the comment correctly:

if (hours_worked[count] > STD_HOURS) /* calculation for gross pay        to include overtime *`

and this line has a bad character at the end:

printf("Enter the number of hours worked for employee #%li: ",      clock_id[count]);`

I'll edit them in the question for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM