Question

In: Computer Science

Question 2 Write a definition of a struct called Employee, which contains three fields: employment_id, int;...

Question 2

  1. Write a definition of a struct called Employee, which contains three fields:

employment_id, int; salary, float; marriage_status, bool.

  1. Given the above Employee struct, declare a variable x with this data type, and assign the three fields, respectively, to the values: 1234, 34000.00, true .

  1. Create a one-dimension array, y, of this struct data type with a length 4. Pass y into a function: PrintArrayElement( …).   Finish the interface of this function, and loop over each array element inside this function.

  1. Write a function printAttribute( ) inside struct Employee. The function should print out the value of each element along with its array index. In addition, it also print out the sum of salary for all the elements. Give a coding example of calling this function in main( ).

Solutions

Expert Solution

#include<bits/stdc++.h>
#include<climits>
using namespace std;

struct Employee
{
        int employment_id;
        float salary;
        bool marriage_status;

        void printAttribute(int i) {
                cout << i << " " << "Emp id " << employment_id << " Salary " << salary << " marriage_status  " << marriage_status << endl;
        }
};
// Driver program to test above functions
void PrintArrayElement(struct Employee y[]) {
        double ans = 0;
        for (int i = 0; i < 4; i++) {
                ans += y[i].salary;
                y->printAttribute(i);
        }
        cout << "The sum of salary of each of the attribute is " << ans << "\n";
}
int main()
{
        struct Employee e = {1234, 34000.00, true}; //initialising the struct
        struct Employee y[4];
        for (int i = 0; i < 4; i++) {
                y[i].employment_id = i + 1;
                y[i].marriage_status = 1;
                y[i].salary = 34000.00;

        }
        PrintArrayElement(y);

        // rest of the code
}

Example run

if you like the answer, please consider upvoting. ;-)


Related Solutions

c++ Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year,...
c++ Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year, int day. Make another struct Product that would contain the following variables: 1- Product name. 2- An array for the product ingredients. Set its max size to 3. 3- Product date of expiration. Use the date struct you made. Use those structs in entering the data for two products of your choice. Make a function printProduct(Product product) that prints out the contents of the...
The tbl_employee contains information about the employee and its department, and it contains the following fields...
The tbl_employee contains information about the employee and its department, and it contains the following fields (it is possible to also include other missing fields): employee_id, employee_gender ,employee_salary, employee_annual_sallary, department_name, department_location, department head. Make use of the database normalization skills learned to normalize a database to the third normalization form. Organize the columns and tables to reduce data redundancy and arrange attributes based on the relationships. Provide a screen shot of the database design before the normalization. Create a heading...
Given: struct Person { int id;     int stats[3] }; Which is the correct way to...
Given: struct Person { int id;     int stats[3] }; Which is the correct way to initialise an array of Persons? 1. struct Person persons[2] = {7, "8,9,3", 8, "2,5,9"}; 2. struct Person persons[2] = "7, {8,9,3}, 8, {2,5,9}"; 3. struct Person persons[2] = "7, "8,9,3", 8, "2,5,9"; 4. struct Person persons[2] = {7, {8,9,3}, 8, {2,5,9}}; Which of the following is not a primitive type in the C language? 1. string 2. int 3. long 4. char Given: struct...
Write a Java class called Employee (Parts of the code is given below), which has three...
Write a Java class called Employee (Parts of the code is given below), which has three private fields firstName (String), lastName (String) and yearsEmployed (double). Implement accessor/mutator methods for the private fields and toString() method, that returns a String representation, which contains employee name and years she was employed. public class Employee { private String firstName; ... } Write a client program called EmployeeClient that creates an instance of Employee class, sets values for the fields (name: John Doe, yearsEmployed:...
C++ language. struct Node {    int data;    Node *next; } Write a function to...
C++ language. struct Node {    int data;    Node *next; } Write a function to concatenate two linked lists. Given lists A* = (4, 6) and B* = (3, 7, 12), after return from Concatenate_Lists(Node A*, Node B*) the list A should be changed to be A = (4, 6, 3, 7, 12). Your function should not change B and should not directly link nodes from A to B (i.e. the nodes inserted into A should be copies of...
Question 2: Design a class named Square that contains data fields side and surfaceArea, and a...
Question 2: Design a class named Square that contains data fields side and surfaceArea, and a method called getSurfaceArea(). Create a child class named Cube. Cube contains a getCubeSurfaceArea() method that calculates the cube surface area. Write a program called DemoSquare that instantiates a Square object and a Cube object and displays the surface areas of the objects. Use the methods created to set the value of the side, and return the surface area. Hint: square area= side*side, Cube surface...
Write a java class called circle that represents a circle. It should have following three fields:...
Write a java class called circle that represents a circle. It should have following three fields: int x (x-coordinate), int y (y-coordinate), double radius (radius of the circle). Your circle object should have following methods: public int getRadius () public int getX () public int getY () public double area () public double perimeter() public String toString() write a client program called CircleClient that creates objects of the circle class called c1 and c2. Assign values to the fields when...
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts...
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts any base(startBase) to another (endBase) by first converting the start base to decimal then to the end base. Do not use library. (bases 1-36 only)
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT