Menu

Array Issue

TrakerJon
2009-02-27
2012-09-26
  • TrakerJon

    TrakerJon - 2009-02-27

    Hey Guys,

    This is homework and I'm stumped...the program seems to work except for the last employees gross. The gross should display as 0.0 but shows 334.00 instead. The hour entries are 51.0, 42.5, 37.0, 45.0 and 0.0. Any help is greatly appreciated...I know I'm close but I'm getting cross-eyed at this point.

    include <stdio.h>

    / Define Symbolic Constants /

    define EMP_NUM 5 / Define number of employees as 5 /

    define OT_RATE 1.5 / Define overtime as 1.5 /

    define STD_HOURS 40.0 / Define standard time as 40 /

    / Declare Functions /

    float collect_Hours (int clock)
    {
    float hours;
    printf("Enter employee hours for clock # %06lu: ", clock);
    scanf("%f", &hours);
    printf("\n");
    return(hours);
    }

    float calc_Overtime (float hours)
    {
    if (hours > STD_HOURS)
    return (hours - STD_HOURS);
    else
    return (0.0);
    }

    float calc_Gross (float hours, float overtime, float wage)
    {
    float gross;
    gross = (STD_HOURS * wage);

    if (overtime &gt; 0)
        return (gross + (OT_RATE * wage) * overtime);
    else
        return gross;
    }
    

    void output (int clock, float wage, float hours, float overtime, float gross)
    {
    printf("\n---------------------------------------------\n");
    printf("Clock # Wage Hours OT Gross");
    printf("\n---------------------------------------------\n\n");
    printf("%06lu %5.2f %3.1f %3.1f %7.2f", clock, wage, hours, overtime, gross);
    printf("\n");
    }

    int main(void)

    {
    / Declare Variables /

    int clock[EMP_NUM] = {98401, 526488, 765349, 34645, 127615}; /* Employee clock # Array */
    int emp_Count[EMP_NUM];                                  /* Number of employ Array */
    int entry;                                                       /* Index Variable */
    float gross[EMP_NUM];                                                /* Gross Array */
    float hours[EMP_NUM];                                           /* Hours Array */
    float overtime[EMP_NUM];                                            /* Overtime Array */
    float wage[EMP_NUM] = {10.60, 9.75, 10.50, 12.25, 8.35};          /* Employee wage Array */
    

    / Program Message /

    printf("\nThis program will calculate five employees wages.\n\n");

    for(entry = 0; entry < EMP_NUM; entry ++)
    {
    / Function to get input from user /

        hours[entry] = collect_Hours (clock[entry]);
    
        /* Function to calculate overtime hours */
    
        overtime[entry] = calc_Overtime (hours[entry]);
    
        /* Separate hours from overtime hours */
    
        hours[entry] -= overtime[entry];
    
        /* Function to calculate gross pay */
    
        gross[entry] = calc_Gross (hours[entry], overtime[entry], wage[entry]);
    

    }

    for(entry = 0; entry < EMP_NUM; entry ++)
    {
    / Function to output employee information to the screen /

        output (clock[entry], wage[entry], hours[entry], overtime[entry], gross[entry]);
    

    }

    return (0);

    }

     
    • TrakerJon

      TrakerJon - 2009-02-27

      I figured it out...missing an if else statement in calc_Gross.

      Thanks!

       
    • Wayne Keen

      Wayne Keen - 2009-02-27

      I think you need to look at this logic again:

      float calc_Gross (float hours, float overtime, float wage)
      {
      float gross;
      gross = (STD_HOURS * wage);

      if (overtime > 0)
      return (gross + (OT_RATE * wage) * overtime);
      else
      return gross;
      }

      It looks to me like you are starting with an assumption that everyone works STD_HOURS, at least in the calculation

      Wayne

       
    • Wayne Keen

      Wayne Keen - 2009-02-27

      Might the appropriate variable in:

      gross = (STD_HOURS * wage);

      Not be the constant STD_HOURS, but perhaps something like
      hours?

      A suggestion. You made your hourly rates non-simple numbers.
      Now,that may be what the problem asked for, but wheen you are building and debugging code, you are probably better off putting some test numbers, like 10 in, so you see right away if
      your calculation is going off.

      I actually maade everyone 10.0 for their rate, and plugged in 30,40,50,60,70 for hours to test my correction of your calculation, so I could see the overtime effect kick in.

      Wayne

       
    • TrakerJon

      TrakerJon - 2009-02-27

      Hey Wayne,

      I understand what you're saying but the program requirements were to use a symbolic constant for the standard hours. Agreed, the logic is off but it seems to work for every employee up until the last employee entry and fails only if that employee has less than 40 hours?

       
      • Wayne Keen

        Wayne Keen - 2009-02-27

        I think your analysis is off. It will fail for EVERY employee with less than 40 hours, as it gives credit to every employee for 40 hours, even if they work 0 hours.

        Wayne

         
        • Wayne Keen

          Wayne Keen - 2009-02-27

          Try plugging in 0 hours for everyone and see what happpens.

          Wayne

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.