Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help! C programing. Get build messges for my get_stats function. Also my gross doesn't calculate correctly. Trying to add a total of all the

Need help! C programing. Get build messges for my get_stats function. Also my gross doesn't calculate correctly. Trying to add a total of all the coulums (ex total ot hours, gross, etc) and average under my table using DYNAMIC LINKED LISTS WITH POINTERS however I keep getting the below message:

||In function 'grossPay':

unused variable 'ot_pay' [-Wunused-variable]| .c||In function 'get_stats':| invalid operands to binary / (have 'float' and 'struct employee *')| invalid operands to binary / (have 'float' and 'struct employee *')| invalid operands to binary / (have 'float' and 'struct employee *')| : invalid operands to binary / (have 'float' and 'struct employee *')| 'my_totals' undeclared (first use in this function)| each undeclared identifier is reported only once for each function it appears in|

Assignment:

The program should prompt the user to enter the number of hours each employee worked. The output should be similar to your previous homework.

The program determines the overtime hours (anything over 40 hours), the gross pay and then outputs a table in the following format. Column alignment, leading zeros in Clock#, and zero suppression in float fields is important. Use 1.5 as the overtime pay factor.

You should implement this program using the following structure to store the information for each employee.

struct employee { char first_name [10]; char last_name [10]; int id_number; /* use can use long int if you wish */ float wage; float hours; float overtime; float gross; struct employee *next; };

Create a linked list of structures using the following data: Connie Cobol 98401 10.60 Mary Apl 526488 9.75 Frank Fortran 765349 10.50 Jeff Ada 34645 12.25 Anton Pascal 127615 8.35 Unlike previous homework, you need to prompt the user for all of the above information, ... and you still need to prompt for the hours worked for each employee.

Hint: Use one or two scanf statements to read in the first and last names with the %s format.

Get the data above from the terminal, and for each one:

get dynamic memory, using malloc, for an employee node

put the employee data in the dynamic memory node

link the nodes with pointers in the above order

After the list pointers are in place you must get at the second and later instances of the structure by going from the first structure down the chain of list pointers.

Then, for each employee, read in the hours worked from the terminal. Do all appropriate computations, and write out the table. You do not need an array of structures like you used in homework 6 and 7. Use the template and dynamically allocate linked list nodes as needed. Similar to the previous homework assignment: a) Add a Total row at the end to sum up the hours, overtime, and gross columns b) Add an Average row to print out the average of the hours, overtime, and gross columns.

Your code should work for any number of employees, and that is how the template is designed.

here is my code::

#include #include /* for malloc */ #include

#define OVERTIME_RATE 1.5f /* Overtime Rate */ #define STD_WORK_WEEK 40.0f /* Stand weekly hours */

struct employee { char first_name [10]; char last_name [10]; int id_number; float wage; float hours; float overtime; float gross;

struct employee *next; };

struct stats { float total_wage; float total_hours; float total_ot; float total_gross; float avg_wage; float avg_hours; float avg_ot; float avg_gross; };

/* TODO - Add Function Prototypes as needed */ void getOT (struct employee *emp1); void grossPay (struct employee *emp1); void get_stats (struct employee *emp1, struct stats);

/* TODO - Add Functions here as needed */

//**************************************************************/ // Function: getOT // // Purpose: Calculates overtime hours // // Parameters: // // *emp_ptr - pointer to array of structures // size - number of employees to process // // Returns: Nothing (call by reference) // //**************************************************************/

void getOT (struct employee *emp1) { int i=0; /*loop index*/ struct employee *tmp; /* tmp pointer value to current node */

/* Process each employee one at a time */ for(tmp = emp1; tmp ; tmp = tmp->next) { if (tmp->hours > STD_WORK_WEEK) /* ot */ { tmp->overtime = tmp->hours - STD_WORK_WEEK; } else /* no ot */ { tmp->overtime = 0; }

i++;/*process each employee */

} /*end loop */ }

//**************************************************************/ // Function: grossPay // // Purpose: Calculates gross pay // // Parameters: // // *emp_ptr - pointer to array of structures // size - number of employees to process // // Returns: Nothing (call by reference) // //**************************************************************/

void grossPay (struct employee *emp1) { int i; /* loop index */ struct employee *tmp; /* tmp pointer value to current node */ float ot_pay; /* local variable */

/* Process each employee one at a time */ for(tmp = emp1; tmp ; tmp = tmp->next) {

/* Calculate Gross Pay */ if (tmp->hours > STD_WORK_WEEK) /* ot */ { tmp->overtime = (tmp->hours - STD_WORK_WEEK); tmp->gross = (STD_WORK_WEEK * tmp->wage) + (tmp->overtime * tmp->wage * OVERTIME_RATE); } else /* no ot */ { tmp->overtime = 0; tmp->gross = (tmp->wage * tmp->hours); }

i++;/*process each employee */

} /* end loop */ } //*******************************************************************/ // Function: get_stats // // Purpose: Outputs to screen in a table format the following // information about an employee: Total and average // // Parameters: // // employeeData - an array of structures containing // employee information // my_totals- array to calculate total and average // size - number of employees to process // // Returns: Nothing (void) // //******************************************************************/ void get_stats(struct employee *emp1, struct stats my_totals) { int i; /* loop index */ float total_wage; /* total wages */ float total_hours; /* total hours */ float total_ot; /* total overtime */ float total_gross; /* total gross pay */ float avg_wage; /* average of wages */ float avg_hours; /* average of hours */ float avg_ot; /* average of overtime */ float avg_gross; /* average of gross pay */ struct employee *tmp; /* tmp pointer value to current node */

for(tmp = emp1; tmp ; tmp = tmp->next) /* any loop is fine */ { i++;/*process each employee */ /* calculate total and average */ total_wage += tmp->wage; total_hours += tmp->hours; total_ot += tmp->overtime; total_gross += tmp->gross; avg_wage= tmp->wage / tmp; avg_hours= tmp->hours / tmp; avg_ot= tmp->overtime / tmp; avg_gross= tmp->gross / tmp; }

printf("Total:"); printf("%30.2f", tmp->wage); printf("%10.1f", total_hours); printf("%10.1f", total_ot); printf("%10.2f ", total_gross); printf("Average:"); printf("%28.2f", avg_wage); printf("%10.1f", avg_hours); printf("%10.1f", avg_ot); printf("%10.2f ", avg_gross);

}

/*-----------------------------------------------------------------------------*/ /* */ /* FUNCTION: print_list */ /* */ /* DESCRIPTION: This function will print the contents of a linked */ /* list. It will traverse the list from beginning to the */ /* end, printing the contents at each node. */ /* */ /* PARAMETERS: emp1 - pointer to a linked list */ /* */ /* OUTPUTS: None */ /* */ /* CALLS: None */ /* */ /*-----------------------------------------------------------------------------*/ void print_list(struct employee *emp1) { struct employee *tmp; /* tmp pointer value to current node */ int i = 0;/* counts the nodes printed */

/* Start a beginning of list and print out each value */ /* loop until tmp points to null (remember null is 0 or false) */ for(tmp = emp1; tmp ; tmp = tmp->next) { i++;

/* TODO - print other members as well */ printf("%-10s", tmp->first_name); printf("%-10s", tmp->last_name); printf("%-6li", tmp->id_number); printf("%10.2f", tmp->wage); printf("%10.1f", tmp->hours); printf("%10.1f", tmp->overtime); printf("%10.2f", tmp->gross); printf(" ");

}

printf(" Total Number of Employees = %d ", i);

}

/*----------------------------------------------------------------------------*/ /* */ /* FUNCTION: main */ /* */ /* DESCRIPTION: This function will prompt the user for an employee */ /* id and wage until the user indicates they are finished. */ /* At that point, a list of id and wages will be */ /* generated. */ /* */ /* PARAMETERS: None */ /* */ /* OUTPUTS: None */ /* */ /* CALLS: print_list */ /* */ /*----------------------------------------------------------------------------*/ int main () {

char answer[80]; /* to see if the user wants to add more employees */ int more_data = 1; /* flag to check if another employee is to be processed */ char value; /* gets the first character of answer */

struct employee *current_ptr, /* pointer to current node */ *head_ptr; /* always points to first node */ struct stats; /* Set up storage for first node */ head_ptr = (struct employee *) malloc (sizeof(struct employee)); current_ptr = head_ptr;

while (more_data) {

/* TODO - Prompt for Employee Name and Hours as well here */ printf(" Please enter Employee Name:"); scanf("%s %s", & current_ptr -> first_name ,& current_ptr -> last_name);

printf(" Enter how many hours worked:"); scanf("%f", & current_ptr -> hours);

/* Read in Employee ID and Hourly Wage */ printf(" Enter employee ID: "); scanf("%i", & current_ptr -> id_number);

printf(" Enter employee hourly wage: "); scanf("%f", & current_ptr -> wage);

/* TODO - Call Function(s) to calculate Overtime and Gross */ /* Function call to calculate overtime */ getOT (head_ptr); /* Function call to calculate gross pay */ grossPay(head_ptr);

printf("Would you like to add another employee? (y/n): "); scanf("%s", answer);

/* Ask user if they want to add another employee */ if ((value = toupper(answer[0])) != 'Y') { current_ptr->next = (struct employee *) NULL; more_data = 0; } else { /* set the next pointer of the current node to point to the new node */ current_ptr->next = (struct employee *) malloc (sizeof(struct employee)); /* move the current node pointer to the new node */ current_ptr = current_ptr->next; } } /* while */

/* Column Header*/ printf (" "); printf ("------------------------------------------------------------------- "); printf ("Name clock# Wage Hours OT Gross "); printf ("------------------------------------------------------------------- "); /* print out listing of all employee id's and wages that were entered */ print_list(head_ptr);

/* Function to call total and average */ get_stats( head_ptr, my_totals);

printf(" End of program "); return (0); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

Students also viewed these Databases questions

Question

LO 5.3 Describe the factors that affect information search.

Answered: 1 week ago

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago