![](/img/trans.png)
[英]I don't know why but the 2nd for loop for printing the id's is not working properly and a sometimes if I give length 9 to 3rd employee program exits
[英]Calculations in 2nd and 3rd function not working properly
問題出在第二和第三函數中,其中“ gross_pay”數組被調用來起作用。 盡我所能想到的一切,我似乎都無法獲得“總薪資”。
我以為我可能已經傳遞了太多的數組來起作用,但似乎需要它們來完成我的數據。 每次我編輯代碼時,都會破壞所有進度。 如果我不傳遞給函數,則可以正確完成所有計算,但是需要在此代碼中引入幾個函數。
任何意見或建議,表示贊賞。
#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 */
這行:
float hourly_Wage[0]; /* initialize array */
創建一個大小為0
的數組。 您可能想要:
float hourly_Wage[SIZE]; /* initialize array */
如果您想在這里定義它。 但是,那么您就沒有價值了。 使用您的模型來傳入和傳出數組,當您調用overtime_grosspay()
時,您可能希望傳入hourly_wage
。我已經修復了我的代碼副本,現在看來可以了。
還有一些錯別字,可能是您粘貼到StackOverflow中時遇到的:
此行無法正確關閉注釋:
if (hours_worked[count] > STD_HOURS) /* calculation for gross pay to include overtime *`
這行結尾處有一個壞字符:
printf("Enter the number of hours worked for employee #%li: ", clock_id[count]);`
我將在您的問題中對其進行編輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.