Question
find_struct.c ////////////////////// // myStats.cpp /* The file consists of the following stats funcions: */ #include stdio.h #include stdlib.h #include string.h #define MAX_EMPLOYEES 200 #define NAME_SIZE
find_struct.c //////////////////////
// myStats.cpp /* The file consists of the following stats funcions: */
#include "stdio.h" #include "stdlib.h" #include "string.h"
#define MAX_EMPLOYEES 200 #define NAME_SIZE 16
struct emp{ char firstName[NAME_SIZE]; char familyName[NAME_SIZE]; float salary; float yearsWithCompany; };
void populateEmployees(struct emp *emp, int numEmployees);
int main(int argc, char* argv[]) { struct emp emp[MAX_EMPLOYEES];
int i; int rc;
populateEmployees(emp, MAX_EMPLOYEES);
// add code to search for employee against the family name "Carp"
// if found print the reocrd
// add code to search for employee against the family name "King"
// if found print the reocrd
return 0; }
/**************************************************************/ /* Purpose: compare the employee reocrd with respect to family name
Input emp - an employee reocrd familyName - the key for searching an employee
Output None
Return 0 if the family name in the employee record does not match that of the given key 1 if the family name in the employee record matches that of the given key */
void cmpEmployee(struct emp *emp, char *familyName)
{
// add code // use the -> opertor to access the fields
// use your comparison functino to compare the strings
}
/**************************************************************/ /* populates the array of employees input numEmployees - number of employees emp - an array of emplyees output emp - array of employees assumption: numEmployees is
char *fn[NUM_NAMES] = {"John", "Jane", "David", "Dina", "Justin","Jennifer", "Don"}; char *sn[NUM_NAMES] = {"Smith", "Johnson", "Mart", "Carp", "Farmer","Ouster","Door"};
empEnd = emp + numEmployees;
while(emp salary = rand() % 30000; emp->yearsWithCompany = rand() % 30; j = rand() % NUM_NAMES; strncpy(emp->firstName, fn[j],NAME_SIZE-1); j = rand() % NUM_NAMES; strncpy(emp->familyName, sn[j],sizeof(emp->familyName)-1); emp++; } }
4 Problem 2: Searching in an array Purpose: a. passing structures as pointers, b. accessing structure fields using "->" operator c. using pointer arithmetic d. taking advantage of "call by value" To do: The file find_struct.c contains a declaration of a struct employee, a main function and a function that populates an array of employees. Review the code and make sure that you understand it. 1. Code a function that compares a single employee record against a given key (in this case it is a family name). The function specifications are: 2. Prototype: void cmpEmployee (struct emp *p, char familyName) input: familyName family name of employee to be searched p a pointer to an employee record Output: None Return: 0 if family name of employee in the provided record does not match the familyName 1 - if family name of employee in the provided record matches the familyName Note 1: use the function myStrCmp() or myStrCmp_2() from Sections 1 and Error! Reference source not found. to compare the keys Note 2: function prototype is already in the C file Note 3: use the operator->to access the fields inside struct emp 3. Code a function that searches the array emp for an employee by family name The function specifications are Prototype struct emp * findEmployee(struct emp *arr, int arraySize, char *familyName); 4. input arr an array of employees arraySize -the number of elements in the array familyName familyName to be used as a key Output: None Return NULL if no matching record was found a pointer to a struct in the array that matches the family name Pseudo Code // iteratively traverse the array using pointer arithmetic. Namely by augmenting the value of // arr by one at every iteration. Note that here we take advantage that the pointer is // a call by value // compare the family name of the record with the key that was given // if a record with a matching name is found then print the record (see below) Record printing firstName familyName salary- years of service Dina Door salary- 28500.00 years of service 9.00 5. Call the function from main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
![blur-text-image](https://dsd5zvtm8ll6.cloudfront.net/includes/images/document_product_info/blur-text-image.webp)
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
![blur-text-image](https://dsd5zvtm8ll6.cloudfront.net/includes/images/document_product_info/blur-subtext-image.webp)
Step: 3
![blur-text-image](https://dsd5zvtm8ll6.cloudfront.net/includes/images/document_product_info/blur-subtext-image.webp)
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started