Question

For the following questions, use the definitions of the given structure. struct Date {       int yy,...

For the following questions, use the definitions of the given structure.

struct Date

{

      int yy, mm, dd;

};

struct Emp

{

      char EmpName[25];

      float Salary;

      struct Date hired;

};

struct Dep

{

      struct Emp manager;

      struct Emp worker[25];

      float Profits;

};

  1. Define a struct Date variable called Date1 and initialize it to February 25, 1957, in the correct format.
  2. Define a struct Emp variable called Person1 and initialize it to “Roger”, with a salary of $50,000, who was hired on March 10, 2001.
  3. Define a struct Dep variable called Toys whose manager is “Roger”, from question 2 above and the profits are $80,000. This department has only two employees as shown below:

“Mohave” with a salary of 10,000 and the date of hire being April 20, 2002

“Hunter” with a salary of 8,000 and the date of hire being June 12, 2000

  1. Write a function called PrintEmp() that will receive a structure of type struct Emp and print its EmpName, Salary and Date hired. Calling this function three times and using an extra printf(), print the contents of Toys.
  2. Write a complete C program to implement question 1 — 4 above, and test your program.

Homework Answers

Answer #1

Greetings!

Note: 1. Right side part of screenshot is an output console.

2. Code is lengthy but not difficult, code of previous module has been used in the next module.

3. In case of any doubts, please write it in the comment section.

Ques.1 and 2: (club problem1 and 2 in a single code)

1. Define a struct Date variable called Date1 and initialize it to February 25, 1957, in the correct format.

2. Define a struct Emp variable called Person1 and initialize it to “Roger”, with a salary of $50,000, who was hired on March 10, 2001.

//Typed code

#include <stdio.h>

//Question 1

struct Date

{ int yy, mm, dd};

//Question2

struct Emp

{

char EmpName[25];

float Salary;

struct Date hired;

};

int main()

{ printf("<---------------Question 1--------------->\n");

   struct Date Date1 ={1957, 02,25};

   printf("Hired date: %d-%d-%d", Date1.mm, Date1.dd, Date1.yy);

   printf("\n<-------------Question2------------------>\n");

struct Emp person1 = {"Roger",50000, 2001, 03, 10};

   printf("Hired date: %d-%d-%d \nEmployee Name: %s\nSalary: $%.2f ",person1.hired.mm, person1.hired.dd, person1.hired.yy, person1.EmpName, person1.Salary);

}

//Screenshot of the code with output

3. Define a struct Dep variable called Toys whose manager is “Roger”, from question 2 above and the profits are $80,000. This department has only two employees as shown below:

“Mohave” with a salary of 10,000 and the date of hire being April 20, 2002

“Hunter” with a salary of 8,000 and the date of hire being June 12, 2000

Typed code:

#include <stdio.h>

struct Date

{

    int yy, mm, dd;

};

struct Emp

{

    char EmpName[25];

    float Salary;

    struct Date hired;

};

struct Dep

{

    struct Emp manager;

    struct Emp worker[25];

    float Profits;

};

void main(void)

{

struct Dep Toys = {{"Roger"}, {{"Mohave"},{"Hunter"}},80000};

    Toys.worker[0].hired.mm = 4;

    Toys.worker[0].hired.dd = 20;

    Toys.worker[0].hired.yy = 2002;

    Toys.worker[0].Salary = 10000;

    Toys.worker[1].hired.mm = 6;

    Toys.worker[1].hired.dd = 12;

    Toys.worker[1].hired.yy = 2000;

    Toys.worker[1].Salary = 8000;

printf("Toy Departpnet's manager: %s Profits: $%.2f", Toys.manager.EmpName, Toys.Profits);

printf("\n\nHired date: %d-%d-%d \nEmployee Name: %s\nSalary: $%.2f ",Toys.worker[0].hired.mm, Toys.worker[0].hired.dd, Toys.worker[0].hired.yy, Toys.worker[0].EmpName, Toys.worker[0].Salary);

printf("\n\nHired date: %d-%d-%d \nEmployee Name: %s\nSalary: $%.2f ",Toys.worker[1].hired.mm, Toys.worker[1].hired.dd, Toys.worker[1].hired.yy, Toys.worker[1].EmpName, Toys.worker[1].Salary);

}

Screenshot of the code with output:

4. Write a function called PrintEmp() that will receive a structure of type struct Emp and print its EmpName, Salary and Date hired. Calling this function three times and using an extra printf(), print the contents of Toys.

Typed Code:

#include <stdio.h>

struct Date

{

    int yy, mm, dd;

};

struct Emp

{

    char EmpName[25];

    float Salary;

    struct Date hired;

};

struct Dep

{

    struct Emp manager;

    struct Emp worker[25];

    float Profits;

};

void main(void)

{

struct Dep Toys = {{"Roger"}, {{"Mohave"},{"Hunter"}},80000};

struct Emp employees[3] = {{"Roger",0, {0,0,0}}, {"Mohave", 10000, {2002, 4, 20}}, {"Hunter", 8000, {2000,6,12}}};

    Toys.worker[0].hired.mm = 4;

    Toys.worker[0].hired.dd = 20;

    Toys.worker[0].hired.yy = 2002;

    Toys.worker[0].Salary = 10000;

    Toys.worker[1].hired.mm = 6;

    Toys.worker[1].hired.dd = 12;

    Toys.worker[1].hired.yy = 2000;

    Toys.worker[1].Salary = 8000;

    PrintEmp(employees[0]);

    printf("\n");

    PrintEmp(employees[1]);

    printf("\n");

    PrintEmp(employees[2]);

    printf("\n");

//Toy Dep information (manager,profit and worker 1 & 2 hired date)

printf("\n\nToy Departpnet's manager: %s Profits: $%.2f", Toys.manager.EmpName, Toys.Profits);

printf("\n\nHired date: %d-%d-%d \nEmployee Name: %s\nSalary: $%.2f ",Toys.worker[0].hired.mm, Toys.worker[0].hired.dd, Toys.worker[0].hired.yy, Toys.worker[0].EmpName, Toys.worker[0].Salary);

printf("\n\nHired date: %d-%d-%d \nEmployee Name: %s\nSalary: $%.2f ",Toys.worker[1].hired.mm, Toys.worker[1].hired.dd, Toys.worker[1].hired.yy, Toys.worker[1].EmpName, Toys.worker[1].Salary);

}

// PrintEmp function that will print employee information

void PrintEmp(struct Emp employees)

{

   printf("\nHired date: %d-%d-%d \nEmployee Name: %s\nSalary: $%.2f ",employees.hired.mm, employees.hired.dd, employees.hired.yy, employees.EmpName, employees.Salary);

}

//Screenshot of the code with output

Know the answer?
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
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT