#include <stdio.h>
/*
* Program: Weekly Pay Calculator
* Purpose:
* - Prompt for hours worked per employee
* - Compute overtime hours, normal pay, overtime pay, and gross pay
* - Print a clean, aligned summary table
*
* Notes:
* - Overtime is paid at 1.5x for hours strictly above 40.0
* - Input validation ensures non-negative numeric hours
*/
#define SIZE 5 // Total number of employees processed
#define STD_HOURS 40.0f // Standard weekly hours before overtime begins
#define OT_RATE 1.5f // Overtime rate (time-and-a-half)
int main(void)
{
// ---------------------------------------------------------------------
// Employee static data (IDs and wage rates)
// ---------------------------------------------------------------------
long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615}; // unique employee IDs
float wageRate [SIZE] = {10.60f, 9.75f, 10.50f, 12.25f, 8.35f}; // hourly wage for each employee
// ---------------------------------------------------------------------
// Arrays for inputs and computed values
// ---------------------------------------------------------------------
float hours [SIZE] = {0}; // hours worked in the week
float overtimeHrs[SIZE] = {0}; // overtime hours (hours above STD_HOURS)
float normalPay [SIZE] = {0}; // pay at regular rate (up to STD_HOURS)
float overtimePay[SIZE] = {0}; // pay at overtime rate
float grossPay [SIZE] = {0}; // total pay = normalPay + overtimePay
// ---------------------------------------------------------------------
// Title
// ---------------------------------------------------------------------
printf("\n*** Pay Calculator ***\n\n");
// ---------------------------------------------------------------------
// INPUT: Prompt for hours per employee (clean, multi-line prompts)
// - Validates numeric and non-negative values
// ---------------------------------------------------------------------
for (int i = 0; i < SIZE; i++) {
printf("------------------------------------------------------------\n");
printf("Employee Information\n");
printf(" Clock Number : %ld\n", clockNumber[i]);
printf(" Hourly Wage : $%.2f\n", wageRate[i]);
printf("Request:\n");
printf(" Please enter the hours worked this week\n");
printf(" (use a dot for decimals, e.g., 40.50): ");
// Read and validate: must be numeric and >= 0.0
while (scanf("%f", &hours[i]) != 1 || hours[i] < 0.0f) {
int ch;
// Clear out the rest of the invalid input line to avoid infinite loops
while ((ch = getchar()) != '\n' && ch != EOF) { /* discard */ }
printf(" -> Invalid input. Enter a non-negative number for hours: ");
}
// Clear any trailing characters on the line (optional safety)
int ch;
while ((ch = getchar()) != '\n' && ch != EOF) { /* discard */ }
}
// ---------------------------------------------------------------------
// PROCESSING: Compute overtime hours and all pay components
// ---------------------------------------------------------------------
for (int i = 0; i < SIZE; i++) {
if (hours[i] > STD_HOURS) {
// Hours above the standard threshold are overtime
overtimeHrs[i] = hours[i] - STD_HOURS;
// Normal pay uses only the standard hours cap
normalPay[i] = STD_HOURS * wageRate[i];
// Overtime pay uses the overtime hours at the OT rate
overtimePay[i] = overtimeHrs[i] * wageRate[i] * OT_RATE;
} else {
// No overtime if hours are within or below the standard threshold
overtimeHrs[i] = 0.0f;
// All hours are paid at the normal rate
normalPay[i] = hours[i] * wageRate[i];
overtimePay[i] = 0.0f;
}
// Total weekly gross pay
grossPay[i] = normalPay[i] + overtimePay[i];
}
// ---------------------------------------------------------------------
// OUTPUT: Print aligned table of results
// ---------------------------------------------------------------------
printf("\n%-10s %8s %8s %8s %12s %12s %12s\n",
"Clock#", "Wage", "Hours", "OT Hrs", "Normal Pay", "OT Pay", "Gross Pay");
printf("--------------------------------------------------------------------------------\n");
for (int i = 0; i < SIZE; i++) {
printf("%-10ld %8.2f %8.2f %8.2f %12.2f %12.2f %12.2f\n",
clockNumber[i],
wageRate[i],
hours[i],
overtimeHrs[i],
normalPay[i],
overtimePay[i],
grossPay[i]);
}
printf("--------------------------------------------------------------------------------\n\n");
return 0;
}