Home Work 8 Computer C Programming

Oct 30th, 2014
DreamIt
Category:
Computer Science
Price: $10 USD

Question description


HOMEWORK 8

DYNAMIC LINKED LISTS WITH POINTERS



Write a C program that will
calculate the gross pay of a set of employees.  Note that
there are two items that will help you with this assignment.



  1) Please use the
template provide in the Homework 8 folder.  Use this template
"as is" to jump start your success in completing this
homework.  It should compile and run right out the box.



  2) Read this PowerPoint presentation that will help you
better understand how the template code works.  Run it in slideshow mode.



  C_Link_Lists1.ppt



WHAT
YOU NEED TO DO:



The program should prompt the user
to enter the number of hours each employee worked. When prompted, key in the
hours shown below.



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.



 
--------------------------------------------------------------------------

 
Name 
Clock#  Wage  Hours 
OT  Gross

  ---------------------------------------------------------------------------

  Connie Cobol  098401 
10.60  51.0  11.0  598.90

  Mary 
Apl 526488  9.75 
42.5  2.5  426.56

  Frank  Fortran 765349 
10.50  37.0  0.0  388.50

  Jeff 
Ada 034645  12.25 
45.0  5.0  581.88

  Anton  Pascal 127615 
8.35  0.0 
0.0  0.00



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];

 
  long  id_number;

 
  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. 



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



Tip:  Use left justification to line up character array name values
... for example:  %-10.10s  or  %-10s



Remember:  Use the Template!



Challenge: 



Reading Chapter 16, see if you can read the information from a file instead of
the screen.  You can skip reading data from the screen is you decide
to do the challenge.



Homework 8 Template



Try this example and use it as a
guide to your last homework (number 8).  You just really need to expand
upon it.



Add the missing members to the struct employee type, and add functions as
needed (such as ones to calculate overtime and gross pay).



I added comments in green below that start with TODO to indicate what needs to
change.



Relax, its OK if you use this code
as is and then expand from it ... you have my permission to use all of it.





#include <stdio.h>



#include <stdlib.h>  /* for malloc */



#include <ctype.h>





struct employee



  {



  int id_number;



  float wage;



 



  /*
TODO - Add other members */


  struct employee *next;



  };





/*-----------------------------------------------------------------------------*/



/* 
*/



/* 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("\nEmployee ID: %6d,
Wage: %8.2f\n",tmp->id_number,



  tmp->wage);



  }





  printf("\n\nTotal Number of
Employees = %d\n", 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 */





  /* 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 */


  /* Read in Employee ID and Hourly Wage */



  printf("\nEnter employee ID:
");



  scanf("%i", & current_ptr
-> id_number);





  printf("\nEnter employee hourly
wage: ");



  scanf("%f", & current_ptr
-> wage);





  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 */





  /* print out listing of all employee id's and
wages that were entered */



  print_list(head_ptr);





  printf("\n\nEnd of program\n");



  return 0;



}


Tutor Answer

(Top Tutor) Daniel C.
(997)
School: Duke University
PREMIUM TUTOR

Studypool has helped 1,244,100 students

5 Reviews


Summary
Quality
Communication
On Time
Value
ashleyisgod
Nov 27th, 2016
" Top quality work from this guy! I'll be back! "
Molly_Moon
Nov 4th, 2016
" AMAZING as always! "
Hemapathy
Oct 13th, 2016
" all I can say is wow very fast work, great work thanks "
BlueOcean
Sep 29th, 2016
" Awesome! Exactly what I wanted. "
kevin12622
Sep 20th, 2016
" Goes above and beyond expectations ! "
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1822 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors