Question

in C language we need to find the following : EXERCISE 3: 1. Write the definition of a structure named employee that contains character array members for an employees fir

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

Here is the required code in C programming language :

I have uploaded the code in repl.it for your reference in case there comes some build issue in your IDE or compiler.

Link for repl.it : https://repl.it/@maximusR9000/Chegg-Employee-In-C-Programming-Langauge

##NOTE :

I have used fgets instead of scanf because scanf is not so good when it comes to implementing real-world problems.

If all the input was taken with the help of scanf then an unusual behavior will occur such as some input was skipped. Because of this behavior scanf is not preferred.


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

#define size 20

struct employee{
   char firstName[20];
   char lastName[20];
   int age;
   double hourSalary;
   char gender;
};

void displayEmployees(struct employee *emp){
   for(int i = 0; i < size; i++){
       printf("Name: %s %s Gender: %c Age: %d Hourly Salary: %lf\n", emp[i].firstName, emp[i].lastName, emp[i].gender, emp[i].age, emp[i].hourSalary);
   }
}

double calculateAverage(struct employee *emp){
   double avg = 0;
   for(int i = 0; i < size; i++)
       avg += emp[i].hourSalary;

   return (double)(avg / size);
}

struct employee getYoungEmployee(struct employee *emp){
   int min = 0;
   for(int i = 1; i < size; i++)
       if(emp[i].age < emp[min].age)
           min = i;

   return emp[min];
}

void setData(struct employee emp){

   char str_age[4], str_gen[4], str_salary[100], str_fname[20], str_lname[20];

   printf("Enter first name: ");
   fgets(str_fname, 20, stdin);
   str_fname[strlen(str_fname) - 1] = '\0';
   strcpy(emp.firstName, str_fname);

   printf("Enter last name: ");
   fgets(str_lname, 20, stdin);
   str_lname[strlen(str_lname) - 1] = '\0';
   strcpy(emp.lastName, str_lname);

   printf("Enter gender: ");
   fgets(str_gen, 4, stdin);
   emp.gender = str_gen[0];

   printf("Enter age: ");
   fgets(str_age, 4, stdin);
   emp.age = strtol(str_age, NULL, 0);

   printf("Enter hourly salary: ");
   fgets(str_salary, 100, stdin);
   emp.hourSalary = strtof(str_salary, NULL);
}


int main() {
   struct employee employees[size]; // Since size = 20

   //Asks user to enter the value of 20 employees
   for(int i = 0; i < size; i++){
       setData(employees[i]);
   }

   displayEmployees(employees);

   printf("\nYoung Employee: \n");
   struct employee youngestEmployee = getYoungEmployee(employees);
   printf("Name: %s %s Gender: %c Age: %d Hourly Salary: %lf\n", youngestEmployee.firstName, youngestEmployee.lastName, youngestEmployee.gender, youngestEmployee.age, youngestEmployee.hourSalary);

   printf("Average Salary: %lf", calculateAverage(employees));

   return 0;
}

#########################

I have tested the code with 2 employee details works perfectly

Here is the result of my testing............

https://Chegg-Employee-In-C-Programming-Langauge.maximusr 9000.repl.run K ? clang-7 -pthread -lm -o main main.c > ./main Ente

Add a comment
Know the answer?
Add Answer to:
in C language we need to find the following : EXERCISE 3: 1. Write the definition...
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
  • Code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; ...

    code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; Specification: In this lab, five functions need to be implemented using the given ADT. 1. Employee* readRecord(FILE*) This function receives a FILE pointer created before. It reads a line from the provided csv file, and creates an Employee struct pointer with the information from that line then returns the pointer back to the calling function. Each line in the provided csv file contains the id, salary,...

  • C++ Program 2 Consider a class Employee with data members: age (an integer), id (an integer)...

    C++ Program 2 Consider a class Employee with data members: age (an integer), id (an integer) and salary (a float), and their corresponding member functions as follows: class Employee { private: int age; int id; float salary: public: Employee; // default constructor: age=0, id=0, and salary=0 void setAge(int x); // let age = x void setId(int x); // let id = x void setSalary(float x); // salary = x int getAge(); // return age int getId; // return id float...

  • Pointer tasks: ( In C++ ) 1. Write a program to store the address of a...

    Pointer tasks: ( In C++ ) 1. Write a program to store the address of a character variable and a float variable. • Print the address of both variables. • Print the values of variable using dereference operator. • Add 2 in float value using pointer. • Print address of character variable using reference operator. 2. Write a program that takes 5 elements in an array and print them in reverse order using pointers. 3. Write to program that takes...

  • C language not C++ 1. Write the statements to do the following: (2 pts) a. Define...

    C language not C++ 1. Write the statements to do the following: (2 pts) a. Define a struct with member variables width, height, topleft x, topleft y all floats). Use a tag to call it Rectangle. b. Declare a variable struct type Rectangle 2. Consider the following variables: struct int x; float y; char zi var1; union nt x; float y; char[20] z;) var2 f float and int are stored using 4 bytes each, what is the size (in bytes)...

  • write the program in c++ Pointen & A???ys: Write u function that is passed a pointer...

    write the program in c++ Pointen & A???ys: Write u function that is passed a pointer to a float array, and the size the array as an int. The function should return a pointer to the maximum element in the array The function should be: int *maxp(float* ap, int size) Write a main() program to lest the function with different input arrays. Grading Criteria (1 point each): Uses cin/cout correct function definition prompts user for input, reads it in correct...

  • c++ only Design a class representing an Employee. The employee would have at least these private...

    c++ only Design a class representing an Employee. The employee would have at least these private fields (Variables): name: string (char array) ID: string (char array) salary: float Type: string (char array) All the variables except for the salary should have their respective setters and getters, and the class should have two constructors, one is the default constructor and the other constructor initializes the name,ID and type of employee with valid user input. The class should have the following additional...

  • Structures in C++ :- 1. Write a program that declares a structure to store the code...

    Structures in C++ :- 1. Write a program that declares a structure to store the code number, salary and grade of an employee. The program defines two structure variables, inputs record of two employees and then displays the record of the employee with more salary. 2. Write a program that declares a structure to store the distance covered by player along with the time taken to cover the distance. The program should input the records of two players and then...

  • I NEED ALL QUESTIONS OR ELSE THUMBS DOWN! C++ Implement a structure Employee. An employee has...

    I NEED ALL QUESTIONS OR ELSE THUMBS DOWN! C++ Implement a structure Employee. An employee has a name (a char *) and a salary (a double). Write a default constructor, a constructor with two parameters (name and salary), and methods char* getName() double getSalary() to return the name and salary. Write a small global function TestEmployee() to test your structure. Creating a new employee. Please type the name: Larry Bird Please specify the salary: 200000 New employee has been created....

  • Using struct.c, as a starting point, implement an application that creates a database of employee personal...

    Using struct.c, as a starting point, implement an application that creates a database of employee personal records. Your implementation should follow these guidelines: the definition of the structure PERSON should be provided in an h-file called person.h that you need to create in the src sub-directory, for the content, refer to the lecture notes, typdef should be used for referencing the PERSON structure, an array employees[] should be declared in the main C file (that is struct.c), a new C...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

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