Question

You are given a file that contains employee data. The first entry in the file indicates...

You are given a file that contains employee data. The first entry in the file indicates the number of employees stored
in the file. After that, each entry in the file consists of the name of the employee, job title, number of promotions since
hiring on, and the years the promotions occured. For example, one possible entry in the employee data file might be
Mario Speedwagon
Systems Engineer
2
2003 2006
which indicates that Mario Speedwagon is a Systems Engineer who has been promoted twice. His promotions occured
in the years 2003 and 2006.
This information will be stored using the following structure
typedef struct Employee_s {
char Name[50];
char JobTitle[50];
int NumberOfPromotions;
int YearsPromoted[6];//A maximum of 6 promotions can occur
}Employee;
Write a C program that
• Asks the user for the name of the data file.
• Determines the number of employees contained in the data file by reading the first entry in the file.
• Dynamically allocates the correct number of array elements for an array of type Employee.
• Loads the data from the file into the array of type Employee. A function must be used to load the data
into an array (i.e. not done in main). Note that the number of promotions vary. Six promotions is the
maximum number of promotions allowed by the company.
• Displays the number of employess and employee names (see Sample Execution).

• Displays information for the employee with the greatest number of years between promotions (see Sample Exe-
cution).

• Before exiting the code, makes sure that the data file is closed and that all dynamically allocated memory is
freed up.
Be careful when scanning strings. When using fgets you should check
• If the first character scanned is ’\n’ then scan again.
• After a successful scan, if the last character scanned is ’\n’ then replace it with a ’\0’

Page 2

A sample execution is shown for the data file EmployeeData Sample.txt containing three entries
3
Mario Speedwagon
Systems Engineer
2
2003 2006
Anna Sthesia
Sr. Systems Engineer
4
2004 2006 2008 2010
Paul Molive
Systems Engineer
3
2004 2009 2013

Sample Code Execution: Red text indicates information entered by the user
Employee Data: Enter in the name of the input file: EmployeeData Sample.txt
There are 3 employees in the file EmployeeData Sample.txt
1 Mario Speedwagon
2 Anna Sthesia
3 Paul Molive
The employee with the longest amount of time between promotions is
Name: Paul Molive
Title: Systems Engineer
5 years between 2009 and 2004

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE

#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct Employee_s {
char Name[50];
char JobTitle[50];
int NumberOfPromotions;
int YearsPromoted[6];//A maximum of 6 promotions can occur
}Employee;

//Reads an input file and stores the data into Employee array
void readEmp(FILE *f, Employee *e, int n);
// to print the employee names
void printEmp(Employee *e, int n);
//To calculate the maximum year gap
void CalcMaxYear(Employee *e, int n);

int main()
{
FILE *f;
int n;
char filename[100], buff[100];
printf("Enter the name of the input file: \n");
scanf("%s", filename);
f = fopen(filename, "r");
fgets(buff, 100, f);

//store the number of employees in the variable n
//subtract '0' so as to convert char to int
n = buff[0] - '0';

//allocate memory dynamically
Employee *EmpArray = (Employee *)malloc(sizeof(Employee)*n);
  
  
//call corresponding functions
readEmp(f, EmpArray, n);
printEmp(EmpArray, n);
CalcMaxYear(EmpArray, n);

//close the file
fclose(f);
  
//deallocate memory
free(EmpArray);

return 0;
}

//Reads an input file and stores the data into Employee array
void readEmp(FILE *f, Employee *e, int n)
{
char buffer[100], *token;
int i, yearCount = 0;

/* for every 4 lines in the file after the first line, this loop reads the lines and puts them in the corresponding structure variable members */
/* here strcspn is used to locate the index where the last occurrence of \n is found and replace that with '\0' */
for(i = 0; i < n; i++)
{
fgets(buffer, 100, f);
if(buffer[0] == '\n') fgets(buffer, 100, f);
buffer[strcspn(buffer, "\n")] = 0;
strcpy(e[i].Name, buffer);

fgets(buffer, 100, f);
if(buffer[0] == '\n') fgets(buffer, 100, f);
buffer[strcspn(buffer, "\n")] = 0;
strcpy(e[i].JobTitle, buffer);

fgets(buffer, 100, f);
if(buffer[0] == '\n') fgets(buffer, 100, f);
buffer[strcspn(buffer, "\n")] = 0;
e[i].NumberOfPromotions = buffer[0] - '0';

/* strtok is used to split the 4th line of each entry into individual words, and the atoi function converts the words to int to give us the appropriate years */
fgets(buffer, 100, f);
if(buffer[0] == '\n') fgets(buffer, 100, f);
buffer[strcspn(buffer, "\n")] = 0;
token = strtok(buffer, " ");
yearCount = 0;
// Keep printing tokens while one of the
// delimiters present in str[].
while (token != NULL) {
e[i].YearsPromoted[yearCount++] = atoi(token);
token = strtok(NULL, " ");
}
}
}

// to print the employee names
void printEmp(Employee *e, int n)
{
int i;
for(i = 0; i < n; i++) printf("%d %s\n", i+1, e[i].Name);
}

//To calculate the maximum year gap
void CalcMaxYear(Employee *e, int n)
{
/* the variable max[n] is an array used to store the max year gap for each employee, the variable grandMax is used to store the max in between the elements of max.
the variable maxIndex is used to store the index of employee with maximum year gap. The variable maxYearIndex is used to store the index of the year of promotion which took place after max number of years for the employee with max yera gap */
  
int i, j, max[n], grandMax, promotions, maxIndex, maxYearIndex;
for(i = 0; i < n; i++)
{
/* we calculate the difference between years from the back */
promotions = e[i].NumberOfPromotions;
max[i] = e[i].YearsPromoted[promotions-1] - e[i].YearsPromoted[promotions-2];
maxYearIndex = promotions-1;
/* this loop calculates the max year gap of each employee */
for(j = promotions-2; j >= 0; j--)
{
if(max[i] < e[i].YearsPromoted[j+1] - e[i].YearsPromoted[j])
{
maxYearIndex = j+1;
max[i] = e[i].YearsPromoted[j+1] - e[i].YearsPromoted[j];
}
}
}
  
grandMax = max[0];
maxIndex = 0;
/* this loop calculates the max year gap out of all employees */
for(i = 1; i < n; i++)
{
if(grandMax < max[i])
{
maxIndex = i;
grandMax = max[i];
}
}

/* print details of the employee */
printf("\nThe Employee with longest years between promotions is: \n");
printf("Name: %s\n", e[maxIndex].Name);
printf("Title: %s\n", e[maxIndex].JobTitle);
printf("%d years between %d and %d", grandMax, e[maxIndex].YearsPromoted[maxYearIndex], e[maxIndex].YearsPromoted[maxYearIndex-1]);
}

OUTPUT

Enter the name of the input file: Sample.txt 1 Mario Speedwagon 2 Anna Sthesia 3 Paul Molive The Employee with longest years

Add a comment
Know the answer?
Add Answer to:
You are given a file that contains employee data. The first entry in the file indicates...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the data file “assignment-07-input.csv. The data contains of 4 different things separated by comma –...

    Consider the data file “assignment-07-input.csv. The data contains of 4 different things separated by comma – origin airport code (3 characters), destination airport code (3 characters), airline code (2 characters), passenger count (integer). There is no header row, so you do not have to deal with it. Write code to do the following: • The program will take 3 extra command line arguments & use a makefile to run. o Input file name o Output file name o Airline code...

  • The following data contains monthly measurements of Lake Superior Cloud Cover. 12 1 13 YEAR JANUARY...

    The following data contains monthly measurements of Lake Superior Cloud Cover. 12 1 13 YEAR JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER OCTOBER NOVEMBER DECEMBER 2000 64.94 64.66 62.39 58.90 60.81 68.83 54.74 57.00 54.93 55.97 83.57 72.65 2001 73.48 74.79 64.84 62.33 55.32 51.33 49.45 43.90 58.80 61.55 69.17 77.90 2002 76.45 68.93 59.03 63.33 57.97 54.37 40.48 48.74 53.10 78.55 79.87 69.81 2003 65.97 58.79 58.61 51.60 43.65 41.33 46.32 42.81 58.07 66.03 78.90 70.35 2004...

  • In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, t...

    In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, then present a menu to the user, and at the end print a final report shown below. You may(should) use the structures you developed for the previous assignment to make it easier to complete this assignment, but it is not required. Required Menu Operations are: Read Students’ data from a file to update the list (refer to sample...

  • Write a program that will accept from the user a text file containing hurricane data along...

    Write a program that will accept from the user a text file containing hurricane data along with an output filename. The data consists of the year, the number of storms, the number of hurricanes, and the damage in millions of US Dollars. The first line contains the word “Years” followed by the first year listed and the last year listed separated by tabs. The following lines contain the data separated by tabs. A sample text file is available in this...

  • Don't attempt if you can't attempt fully, i will dislike and negative comments would be given...

    Don't attempt if you can't attempt fully, i will dislike and negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book titnle, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an HTML web...

  • Question: Evaluate the relationship between cost per visit and week. Interpret your regression re...

    Question: Evaluate the relationship between cost per visit and week. Interpret your regression results by discussing significance of the regression equation and magnitude of the estimated coefficients. TRUE MASTER PLAN True Master Plan (TMP) is a managed care company that provides and finances health care services for employees of DigiTech Media, Inc. Approximately 5,000 employees at DigiTech Media are currently enrolled in TMP's health insurance plan. The number of enrollees has increased over the past year as DigiTech Media continued...

  • Write the C program, to achieve as shown in the sample code execution, using the given...

    Write the C program, to achieve as shown in the sample code execution, using the given struct and using the comments in the given main program below: typedef struct{ char first[20]; char last[20]; float gpa; int score; } student; int main(void){ student *ptr; //first name //last name //student gpa //student score } //ask a user to enter the number of students, num //dynamically allocate memory for an array of students of the appropriate size (i.e. //num that the user just...

  • Please read case article, "Attention Kmart Shoppers? Into and out of Bankruptcy" and help me come...

    Please read case article, "Attention Kmart Shoppers? Into and out of Bankruptcy" and help me come up with a solution for the case as well as action steps to implement the solution! Thank you!! ATTENTION KMART SHOPPERS? Former Kmart CEO, Charles C. Conaway, failed in his 19-month effort to revive the iconic firm, resulting in the largest retailing bankruptcy filing in history on January 22, 2002 (Davies, et al., 2002). On March 11, 2002, bankrupt Kmart named James B. Adamson...

  • internal project 1 anything helps! thank you!! Instructions: Study the case that starts on page 3...

    internal project 1 anything helps! thank you!! Instructions: Study the case that starts on page 3 carefully. Then write concise answers to the following questions regarding the internal control system of Duarf, Inc. Clearly label your responses with proper headings and subheadings. Be very specific and precise. Answers that appear to be beating around the bush will not get any credit. 1. What are the controls in place that under normal conditions should function well to prevent embezzlements or frauds?...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT