I am struggling with the program in C language. --->Create an employee structure
ID: 3676491 • Letter: I
Question
I am struggling with the program in C language. --->Create an employee structure that contains members for the first name, last name, and salary. Use typedef to make an employee_t data type. Create a function called init_employee() that takes an employee_t variable by reference and puts a random string in its first name member, a random string in its last name member, and a random float in its salary member. Create a print_employee() function that takes an employee variable in by reference and prints out its members. In the main() function, ask the user how many employees to create and dynamically allocate an array for employee_t elements of that size. Use the functions on each element on the array and calculate and display the average salary.
Explanation / Answer
This below c code is written based on the given problem statement.
1. Created employee structure with data fields.
2. Created methods for print_employee and init_employee
3. Passed the employee object two methods.
See the code below:
Answer for Question 1:
#include<stdio.h>
typedef struct Employee
{
char f_name[20];
char l_name[20];
float salary;
}employee_t;
void init_employee(employee_t& emp)
{
strcpy(f_name,emp.f_name);
strcpy(l_name,emp.l_name);
salary = emp.salary;
}
void print_employee(employee_t& emp)
{
printf("First Name is :%s",emp.f_name);
printf("Last Name is :%s",emp.l_name);
printf("Salary is :%f",emp.salary);
}
int main()
{
employee_t emp1;
printf("Enter employee first name: ");
scanf("%s",&emp1.f_name);
printf("Enter employee last name: ");
scanf("%s",&emp1.l_name);
printf("Enter salary :");
scanf("%f",&emp1.salary);
init_employee(emp1); // passing structure variable s1 as argument
print_employee(emp1);
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.