Enter the code provided in the assignment Resources in CodeBlocks, compile, and
ID: 3571665 • Letter: E
Question
Enter the code provided in the assignment Resources in CodeBlocks, compile, and run. Pay attention to the formatting of the code. Add at least eight comments to explain what the program does and how it works. Be sure to include a comment at the top of the file as a header like the one used in Unit 1. After adding your comments, build and test the program to make sure it still builds and runs correctly.
#include <stdio.h>
#define DAYS 7
int main(void) {
printf("Enter hourly wage: ");
float wage;
scanf("%f", &wage);
printf("Enter hours for each day in the week: ");
int i = 0;
float hours[DAYS];
for(i = 0; i < DAYS; i++) {
printf("Day %d: ", i + 1);
scanf("%f", &hours[i]);
}
float totalHours = 0;
for(i = 0; i < DAYS; i++) {
totalHours += hours[i];
}
float grossPay = 0;
if(totalHours > 40) {
float overtimeHours = totalHours - 40;
grossPay += overtimeHours * wage * 1.5;
grossPay += wage * 40;
}
else {
grossPay = totalHours * wage;
}
printf("Gross pay: $%.2f", grossPay);
return 0;
}
Explanation / Answer
/* Program to calculate total amount paid to workers on the basis of work hours */
/* inbuilt library of c */
#include <stdio.h>
/* defining days variable to 7 */
#define DAYS 7
int main(void) {
/*prompting user to enter per hour payment */
printf("Enter hourly wage: ");
float wage;
/* taking the input from the user and storing into float variable wage */
scanf("%f", &wage);
/*prompting user to enter working hours for each day in the week */
printf("Enter hours for each day in the week: ");
int i = 0;
/* Array to store number of working hours for each day in the week */
float hours[DAYS];
/* storing the user input in the array hours */
for(i = 0; i < DAYS; i++) {
printf("Day %d: ", i + 1);
scanf("%f", &hours[i]);
}
float totalHours = 0;
/* calculating total working hours in the week and storing the value in the float variable totalHours */
for(i = 0; i < DAYS; i++) {
totalHours += hours[i];
}
/* declaring total payment or gross pay in variable grossPay */
float grossPay = 0;
/* if total working hours is greater than 40 in week then paying extra 1.5 times of wage for overtime hours
and calculating the total payment i.e grossPay */
if(totalHours > 40) {
float overtimeHours = totalHours - 40;
grossPay += overtimeHours * wage * 1.5;
grossPay += wage * 40;
}
/* calculating grossPay if there is no overtimeHours */
else {
grossPay = totalHours * wage;
}
/* printing grossPay */
printf("Gross pay: $%.2f", grossPay);
return 0;
}
/* NOTE: After adding the comments i have run the program its working fine,Please ask in case of any doubt,would glad to help,Thanks !! */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.