2013-10-13 33 views
0

輸出顯示全0。毛重和OT是不計算的。輸出顯示全0。總量和OT不計算

#include <stdio.h> 


#define STD_HOURS 40.0 
#define OT_RATE 1.5 
#define SIZE 5 


void read_hours(float worked_hours[], long int clockNumber[]); 
void calculate_gross(float wage[], float worked_hours[], float gross); 
void calculate_gross_ot(float wage[], float worked_hours[]); 
void printFunction(long int clockNumber[], float wage[], float worked_hours[], float OT[],    float gross); 

int i; 



int main() 
{ 

    long int clockNumber [SIZE] = {98401, 526488, 765349, 34645, 127615};/* employee ID */ 
    float gross = 0.0; /* gross */ 
    float OT [SIZE] = {}; /* overtime */ 
    float wage [SIZE] = {10.6, 9.75, 10.5, 12.25, 8.35};  /* hourly wage */ 
    float worked_hours [SIZE] = {}; // Hours worked 


    read_hours(worked_hours, clockNumber); 

    if (worked_hours[i]>STD_HOURS) 
    { 

    calculate_gross_ot(wage, worked_hours); 

    } 

    else 

    { 
     calculate_gross(wage,worked_hours, gross); 

    } 
    printFunction(clockNumber, wage, worked_hours, OT, gross); 

    return(0); 
} 



void read_hours(float worked_hours[], long int clockNumber[]) 
{ 



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

     printf("\n Enter Hours for Emlployee ID: %ld\n", clockNumber[i]); 
     scanf ("%f", &worked_hours[i]); 

    } 


} 

void calculate_gross(float wage[], float worked_hours[], float gross) 
{ 


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

     gross=(wage[i]*worked_hours[i]); 
} 


void calculate_gross_ot(float wage[], float worked_hours[]) 
{ 


    float gross; 
    float OT[SIZE]; 

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



     /* calculating overtime hours */ 
      OT[i]=worked_hours[i]-STD_HOURS; 

      /* calculating gross pay with overtime */ 
      gross = (STD_HOURS*wage[i]) + (OT[i]*OT_RATE*wage[i]); 
     //} 

    } 

} 

void printFunction(long int clockNumber[], float wage[], float worked_hours[], float OT[], float gross) 
    { 


     /* creating a table for the output */ 
     printf("------------------------------------------------\n"); 
     printf("%7s","Clock#"); 
     printf("%7s","Wages"); 
     printf("%7s","Hours"); 
     printf("%7s","OT"); 
     printf("%7s\n","Gross"); 
     printf("------------------------------------------------\n"); 
for (i=0; i<SIZE; i++) 

{ 
     /* printing the results */ 
     printf("%6ld", clockNumber[i]); 
     printf("%10.2f",wage[i]); 
     printf("%6.1f", worked_hours[i]); 
     printf("%6.1f", OT[i]); 
     printf("%10.2f",gross); 
     printf("\n"); 

} 


    } 

該程序用於計算有和沒有OT小時的毛重。輸出結果顯示毛重和OT全部爲0。請幫忙弄清楚錯誤在哪裏。

+0

你有什麼試過的? StackOverflow旨在幫助您解決問題,而不是解決您的錯誤。 – FeifanZ

回答

3

在第一種情況下,您按值傳遞gross

在第二種情況下,您根本沒有傳遞它(該函數有一個名爲gross的本地函數)。

在這兩種情況下,無論何時相應功能發生變化gross,此更改都不會傳播給調用者。

您需要通過指針傳遞gross,或者使用return語句(並適當更改返回類型)從函數返回值。

0

通過OT到您的calculate_gross_ot()功能從main()

... 
calculate_gross_ot(wage, worked_hours, OT); 
... 


void calculate_gross_ot(float wage[], float worked_hours[], float OT[])