Question

In: Computer Science

C programming A small company provided you three files. 1) emp.txt : this file contains list...

C programming

A small company provided you three files.

1) emp.txt : this file contains list of employees where each line represents data of an employee. An employee has an id (String max length 20), last name (string max length 100), and salary (int). See the example emp.txt file.

2) dept.txt: This text file contains list of departments of the employees. Each line of the file contains an employee id (string max length 20) and department name for that employee (string max length 100). See the example dept.txt file.

3) query.txt: This file contains a set of queries. There can be two types of queries. Query type 1 represents searching for an employee and Query type 2 presents searching for a department. The first line of the file contains the number of queries n and then next n lines contains the queries. The first number of each line represents the type of query and the second string in the line represents the query key. See the example query.txt file.

Example Query:

A query line "1 Smith" will search for Smith and will display details of Smith including his department name. If Smith does not exist, your program should display "Employee Smith Not Found"

A query line "2 Clinical" will search for Clinical department and will display total salary paid to that department. If the department does not exist, the total salary will be 0.

emp dept relationship: Employee and department is related by the employee id. An employ can have 0 or one department. For example, e5 Martin is not associated to any department. A department can have 0 or more employees.

Requirements:

1. Your program need to have at least two structures. One for employee and one for department. You need to use array of those structures.

2. int readEmp(Employee ArrayOfEmployees[]): You need to implement readEmp function with this function header. This function opens your emp.txt file and load the passed array to this function and returns number of employees in the file.

3. void readDept(Dept ArrayOfDepartments[], int *numOfDept): You need to implement readDept function with this function header. This function opens yourdept.txt file and load the passed array to this function and also update the referenced variable with the number of department in the file.

4. void printAll(Employee Arr[], int totalEmp, Dept ArrayOfDepartments[], int totalDept): This function prints the details of all the employees including their department name available in the passed arrays. See the output format from the sample output shown bellow.

5. void search_employee(Employee Arr[], int totalEmp, Dept ArrayOfDepartments[], int totalDept, char qStr[]): This function processes query type 1 and produces output like the sample output.

6. int totalSal_dept(Employee Arr[], int totalEmp, Dept ArrayOfDepartments[], int totalDept, char qStr[]) : This function processes the query type 2 and produces the output like the sample output.

7. In the main function, declare necessary variables and call the load functions first and then call the printAll function. Next, start reading the query file and process the queries by calling appropriate function(s).

Sample Input/Output: Use the input files. There is no console input involved in this program. The output of the code is based on the provided files

Printing the list of Employees:
=================

ID: e1 LName: Adam Salary: 500 Department: Finance

ID: e2 LName: Smith Salary: 700 Department: Sales

ID: e3 LName: Robin Salary: 450 Department: Marketing

ID: e4 LName: Jack Salary: 800 Department: Sales

ID: e5 LName: Martin Salary: 600 Department: None

ID: e6 LName: Nusair Salary: 700 Department: IT

Query Phase

ID: e2 LName: Smith Salary: 700 Department: Sales

Total Salary of Department Clinical is 0

Employee Nasir Not Found

Total Salary of Department Sales is 1500

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXE 50 // For maximum number of employee records
#define MAXD 50 // For maximum number of department records

struct Employee
{
char id[20];
char lastName[100];
int salary;
};

struct Dept
{
char id[20];
char departmentName[100];
};

// Function to read data from file and stores it in array of Employee object
// Returns number of records read from file
int readEmp(struct Employee ArrayOfEmployees[])
{
// To store number of records
int len = 0;

// Creates a file pointer to open the file emp.txt in read mode
FILE *readF = fopen("emp.txt", "r");

// Checks if file is unable to open then display error message
if(readF == NULL)
{
// Display error message
puts("Error: Could not open file: emp.txt");
exit(0);
}// End of if

// Extracts data from file and stores it
while(!feof(readF))
{
// Reads a number from file and stores it at len index position
fscanf(readF, "%s %s %d", ArrayOfEmployees[len].id, ArrayOfEmployees[len].lastName,
&ArrayOfEmployees[len].salary);

// Increase the index counter by one
len = len + 1;
}// End of while loop

// Close the file
fclose(readF);
// Returns number of records
return len;
}// End of function

// Function to read department information from file and stores it in array of Dept object
// Returns number of records using pointer variable *numOfDept
void readDept(struct Dept ArrayOfDepartments[], int *numOfDept)
{
// Creates a file pointer to open the file dept.txt in read mode
FILE *readF = fopen("dept.txt", "r");

// Checks if file is unable to open then display error message
if(readF == NULL)
{
// Display error message
puts("Error: Could not open file: dept.txt");
exit(0);
}// End of if

// Extracts data from file and stores it
while(!feof(readF))
{
// Reads a number from file and stores it at numOfDept index position
fscanf(readF, "%s %s", ArrayOfDepartments[*numOfDept].id, ArrayOfDepartments[*numOfDept].departmentName);

// Increase the index counter by one
*numOfDept = *numOfDept + 1;
}// End of while loop

// Close the file
fclose(readF);
}// End of function

// Function to display all the employee and their corresponding department name information
void printAll(struct Employee Arr[], int totalEmp, struct Dept ArrayOfDepartments[], int totalDept)
{
// Loops variable
int c, d;
// Loops till number of employees
for(c = 0; c < totalEmp; c++)
{
// Loops till number of departments
for(d = 0; d < totalDept; d++)
{
// Checks if current employee id is equals to current department employee id
if(strcmp(Arr[c].id, ArrayOfDepartments[d].id) == 0)
{
// Displays the employee and department information
printf("\n ID: %s LName: %s Salary: %d Department: %s",
Arr[c].id, Arr[c].lastName, Arr[c].salary, ArrayOfDepartments[d].departmentName);
// Come out of the loop
break;
}// End of if condition
}// End of inner for loop
}// End of inner outer loop
}// End of function

// Function to search for an employee name and displays the information if found
// otherwise displays error message
void search_employee(struct Employee Arr[], int totalEmp, struct Dept ArrayOfDepartments[],
int totalDept, char qStr[])
{
// Loops variable
int c, d;
// To store found status. Initially -1 for not found
int foundStatus = -1;

// Loops till number of employees
for(c = 0; c < totalEmp; c++)
{
// Checks if current employee last name is equals to parameter qSrt name
if(strcmp(Arr[c].lastName, qStr) == 0)
{
// Sets the found status to loop variable used for employee (c)
foundStatus = c;
// Loops till number of departments
for(d = 0; d < totalDept; d++)
{
// Checks if current employee id is equals to current department employee id
if(strcmp(Arr[c].id, ArrayOfDepartments[d].id) == 0)
{
// Displays the employee and department information
printf("\n ID: %s LName: %s Salary: %d Department: %s",
Arr[c].id, Arr[c].lastName, Arr[c].salary, ArrayOfDepartments[d].departmentName);
// Come out of the loop
break;
}// End of if condition
}// End of for loop
}// End of outer if condition
}// End of outer for loop

// Checks if found status is -1 then not found
if(foundStatus == -1)
printf("\n Employee %s Not Found", qStr);
}// End of function

// Function to search a department name if found calculate the total salary of all the employees
// in that department and returns it
// Otherwise returns 0
int totalSal_dept(struct Employee Arr[], int totalEmp, struct Dept ArrayOfDepartments[],
int totalDept, char qStr[])
{
// Loops variable
int c, d;
// To store total
int total = 0;
// Loops till number of departments
for(c = 0; c < totalDept; c++)
{
// Checks if current department's department name is equals to parameter qSrt department name
if(strcmp(ArrayOfDepartments[c].departmentName, qStr) == 0)
{
// Loops till number of employees
for(d = 0; d < totalEmp; d++)
{
// Checks if current employee id is equals to current department employee id
if(strcmp(Arr[d].id, ArrayOfDepartments[c].id) == 0)
{
// Calculates total salary
total += Arr[d].salary;
}// End of if condition
}// End of for loop
}// End of outer if condition
}// End of outer for loop

// Returns the calculate total
return total;
}// End of function

// main function definition
int main()
{
// Creates an array of object of Employee of size MAXE
struct Employee arrayOfEmployees[MAXE];
// Creates an array of object of Dept of size MAXE
struct Dept arrayOfDepartments[MAXD];
// To store number of department records
int numOfDept = 0;
// To store number of employee records
int numOfEmp = 0;
// To store number of query records
int numOfQuery = 0;
// To store query type
int queryType;
// To store query
char query[20];
// Loops variable
int c;

// Calls the function to read employee records
numOfEmp = readEmp(arrayOfEmployees);
// Calls the function to read department records
readDept(arrayOfDepartments, &numOfDept);
printf("\n Printing the list of Employees: ");
printf("\n =================");
// Calls the function to display all employee and department information
printAll(arrayOfEmployees, numOfEmp, arrayOfDepartments, numOfDept);
printf("\n\n Query Phase");

// Creates a file pointer to open the file query.txt in read mode
FILE *readF = fopen("query.txt", "r");

// Checks if file is unable to open then display error message
if(readF == NULL)
{
// Display error message
puts("Error: Could not open file: query.txt");
exit(0);
}// End of if

// Reads number of queries
fscanf(readF, "%d", &numOfQuery);

// Loops till number of queries
for(c = 0; c < numOfQuery; c++)
{
// Reads query type and query
fscanf(readF, "%d %s", &queryType, query);

// Checks if query type is 1
if(queryType == 1)
// Calls the function to display employee and its department information
search_employee(arrayOfEmployees, numOfEmp, arrayOfDepartments, numOfDept, query);

// Otherwise checks if query type is 2
else if(queryType == 2)
// Calls the function to calculate total salary for the department
printf("\n Total Salary of Department %s is %d", query,
totalSal_dept(arrayOfEmployees, numOfEmp, arrayOfDepartments, numOfDept, query));

// Otherwise displays error message
else
printf("\n\t Invalid Query Type!!");
}// End of while loop

// Close the file
fclose(readF);
return 0;
}// End of main function

Sample Output:

Printing the list of Employees:
=================
ID: e1 LName: Adam Salary: 500 Department: Finance
ID: e2 LName: Smith Salary: 700 Department: Sales
ID: e3 LName: Robin Salary: 450 Department: Marketing
ID: e4 LName: Jack Salary: 800 Department: Sales
ID: e5 LName: Martin Salary: 600 Department: None
ID: e6 LName: Nusair Salary: 700 Department: IT

Query Phase
ID: e2 LName: Smith Salary: 700 Department: Sales
Total Salary of Department Clinical is 0
Employee Nasir Not Found
Total Salary of Department Sales is 1500

emp.txt file contents

e1 Adam 500
e2 Smith 700
e3 Robin 450
e4 Jack 800
e5 Martin 600
e6 Nusair 700

dept.txt file contents

e1 Finance
e2 Sales
e3 Marketing
e4 Sales
e5 None
e6 IT

query.txt file contents

4
1 Smith
2 Clinical
1 Nasir
2 Sales


Related Solutions

Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...
Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18...
Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18 Stacks and Queues ----------------------------------------------------------------------------------------------------- capacity is just 5 1. push 6 numbers on the stack 2. catch the overflow error in a catch block 3. pop one element, which means your capacity is now down to 4 4. push the element that was rejected earlier 5. verify your entire stack by popping to show the new numbers. IntStack.h #include <memory> using namespace std; class...
Using C programming I have a file that contains earthquake data that I will copy and...
Using C programming I have a file that contains earthquake data that I will copy and paste below. I want to use either bubble or insertion sort to sort the file by latitude in ascending order, then create a new file containing the sorted data. example file to sort: time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net 2020-10-17T17:22:03.840Z,32.877,-116.2991667,0.31,1.16,ml,21,119,0.07747,0.26,ci 2020-10-17T17:17:29.980Z,34.1611667,-116.452,2.75,0.87,ml,17,66,0.05224,0.22,ci 2020-10-17T17:03:54.460Z,33.5396667,-116.4613333,8.66,0.63,ml,18,126,0.06084,0.16,ci 2020-10-17T16:55:01.080Z,63.254,-151.5232,8,1.4,ml,,,,0.9,ak
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class definitions, one base class and three derived classes. The derived classes will have an inheritance relationship (the “is a” relationship) with the base class. You will use base and derived classes. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below. ========================================================= // listmain.cpp #include "Linked_List.h" int main(int argc, char **argv) {      float           f;      Linked_List *theList;      cout << "Simple List Demonstration\n";      cout << "(List implemented as an Array - Do...
Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H...
Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H #define   BINGOBALL_H #include <iostream> class BingoBall { public:    BingoBall():letter{'a'}, number{0} {}    BingoBall(char let, int num) :        letter{ let }, number{ num } {}    char getChar() const { return letter; }    int getNumber() const { return number; }    //overload == operator    bool operator==(BingoBall &b) const { return (number == b.getNumber() && letter == b.getChar()); } private:   ...
Download and review the PrintNumbers.java program provided in the homework files. This program contains a method...
Download and review the PrintNumbers.java program provided in the homework files. This program contains a method which is designed to read in a number from the user and print it to the screen. The parameter to the method specifies whether the number might contain decimals. There are two helper methods- one that reads in an integer and one that reads in a double. Run the program. As written, it works as long as the user enters what they are supposed...
Which of the following files is a temporary file? a. transaction file b. master file c. reference file d. none of the above
Which of the following files is a temporary file? a. transaction fileb. master filec. reference filed. none of the above
A tablet PC contains 3217 music files. The distribution of file size is highly skewed with...
A tablet PC contains 3217 music files. The distribution of file size is highly skewed with many small file sizes. The mean file size on the tablet is 2.35 MB with a standard deviation of 3.25 MB. a What is the probability that the average size of 30 randomly selected music files taken from the tablet is more than 2.75 MB? b What is the probability that the average size of 50 randomly selected music files taken from the tablet...
A tablet PC contains 3217 music files. The distribution of file size is highly skewed with...
A tablet PC contains 3217 music files. The distribution of file size is highly skewed with many small files. Suppose the true mean file size of music and video files on the tab, LaTeX: \mu\:=\:2.30μ = 2.30 MB, and also assume that the standard deviation for this population (LaTeX: \sigmaσ )is 3.25 megabytes (MB). If you select a random sample of 50 files. a. What is the probability that the mean file size of your sample (50 files as described...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT