Question

In: Computer Science

Purpose Review and reinforcement of pointers, dynamic memory allocation, pointer arithmetic, passing pointers to a function,...

Purpose

Review and reinforcement of pointers, dynamic memory allocation, pointer arithmetic, passing pointers to a function, returning a pointer by a function, dangling pointer, and memory deallocation, pointer initialization, and struct data type.

Project description

In this project, you will create a database of employees of an organization while meeting the requirements described below. Your program MUST NOT interact with the user to receive inputs, so that the instructor and/or the teaching assistant can save a big time in testing programs.

  1. Construction of a Database of Employees
  1. Employee

Employee is a struct data type that has the following attributes, or data members, or data fields

struct Employee {

string firstName;

string lastName;

int     SSN;

string department;

Role    role;

double salary;

};

The data type Role is defined as below.

enum Role {programmer, manager, director};

Define Employee.h and include the two data types above in the file. Role must be defined before Employee because Employee uses Role.

  1. Create an array of employees (considered as a database) with the size of 5 by using one single dynamic memory allocation
  2. Set the salaries of employees by defining and calling setSalaries() in which each employee is given a salary with a random number ranging from 45000 to 65000
  3. Set the roles of employees by defining and setRoles() in which each employee is given a random role
  4. In order to fill out other data members of each employee, you can do hardcoding or apply random number generator and/or string manipulation or any of your choice, as long as your program does not interact with the user.
  5. Display on the console information of all employees using a table format

  1. Display of the Statistics
    1. Display the average salary of the employees on the console
    2. Display the information of programmers only on the console

  1. Memory management
  1. Release any dynamically allocated memory in your program without memory leak
  2. Reset any pointer variable if it is no longer used. Otherwise, such a pointer variable becomes a dangling pointer pointing to a memory that has been deleted already.
  1. Readability
    1. Apply indentations appropriately to make your program readable.

  1. Others
  1. No need of classes/objects
  2. No need of makefile

Solutions

Expert Solution

Screenshot

Program

Employee.h

#include<string>
using namespace std;
enum Role { programmer, manager, director };
struct Employee {
   string firstName;
   string lastName;
   int     SSN;
   string department;
   Role    role;
   double salary;
};

Employeemain.cpp

#include <iostream>
#include<iomanip>
#include "Employee.h"
using namespace std;
//Function prototypes
void setSalaries(Employee*,int);
void setRoles(Employee*, int);
void setOther(Employee*, int);
void displayEmployeeIndormation(Employee*, int);
void displayAvgSalry(Employee*, int);
void displayProgrammerInformation(Employee*, int);
int main()
{
    //Create an array of employee
   Employee *employees;
   //Allocate size dynamically
   employees = new Employee[5];
   //Call setter functions
   setSalaries(employees, 5);
   setRoles(employees, 5);
   setOther(employees, 5);
   //Call display functions
   displayEmployeeIndormation(employees, 5);
   displayAvgSalry(employees, 5);
   displayProgrammerInformation(employees, 5);
   //Call memory deallocation
   delete[] employees;
}
//Function to set salary of each employee between 45000 - 65000
void setSalaries(Employee* employees, int cnt) {
   for (int i = 0; i < cnt; i++) {
       employees[i].salary = 45000 + (std::rand() % (65000 - 45000 + 1));
   }
}
//Function to set roles of each employee
void setRoles(Employee* employees, int cnt) {
   for (int i = 0; i < cnt; i++) {
       employees[i].role =Role(rand() % 3);
   }
}
//Function to set Other values of employees
void setOther(Employee* employees, int cnt) {
   char consonents[] = { 'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z' };
   char vowels[] = { 'a','e','i','o','u','y' };
   string departments[] = { "Accounts","Sales","Manufacturing","Technical" };
   for (int i = 0; i < cnt; i++) {
       string name1 = "", name2 = "";
       int random = rand() % 2;
       int count = 0;
       for (int i = 0; i < 5; i++) {

           if (random < 2 && count < 2) {
               name1 = name1 + consonents[rand() % 19];
               name2 = name2 + consonents[rand() % 19];
               count++;
           }
           else {
               name1 = name1 + vowels[rand() % 5];
               name2 = name2 + vowels[rand() % 5];
               count = 0;
           }

           random = rand() % 2;
       }
       employees[i].firstName = name1;
       employees[i].lastName = name2;
       employees[i].SSN = i + 10;
       employees[i].department = departments[rand() % 4];
   }
}
//Function to display all employees information
void displayEmployeeIndormation(Employee* employees, int cnt) {
   cout << "\nEMPLOYEE DETAILS:-" << endl;
   for (int i = 0; i < 74; i++) {
       cout << "-";
   }
   cout << "\n| FirstName    | LastName    | SSN    | Department    | Role     | Salary |" << endl;
   for (int i = 0; i < 74; i++) {
       cout << "-";
   }
   cout << endl;
   for (int i = 0; i < cnt; i++) {
       cout << "| " << setw(8) << employees[i].firstName << setw(6) << "|"
           << setw(8) << employees[i].lastName << setw(6) << "|"
           << setw(4) << employees[i].SSN << setw(5) << "|"
           << setw(8) << employees[i].department << setw(8) << "|";
       if (employees[i].role == 0) {
           cout << setw(10) << "Programmer" << "|";
       }
       else if (employees[i].role == 1) {
           cout << setw(8) << "Manager" << setw(3) << "|";
       }
       else if (employees[i].role == 2) {
           cout << setw(8) << "Director" << setw(3) << "|";
       }
       cout << setw(6) << employees[i].salary << setw(3) << "|" << endl;;
   }
   for (int i = 0; i < 74; i++) {
       cout << "-";
   }
   cout << endl;
}
//Function to display average salary
void displayAvgSalry(Employee* employees, int cnt) {
   double avgSal = 0;
   for (int i = 0; i < cnt; i++) {
       avgSal += employees[i].salary;
   }
   cout << "\nAverage Salary = " << (avgSal / cnt) << endl;
}
//Function to display programmer information
void displayProgrammerInformation(Employee* employees, int cnt) {
   cout << "\nPROGRAMMER DETAILS:-" << endl;
   for (int i = 0; i < 74; i++) {
       cout << "-";
   }
   cout << "\n| FirstName    | LastName    | SSN    | Department    | Role     | Salary |" << endl;
   for (int i = 0; i < 74; i++) {
       cout << "-";
   }
   cout << endl;
   for (int i = 0; i < cnt; i++) {
       if (employees[i].role == 0) {
           cout << "| " << setw(8) << employees[i].firstName << setw(6) << "|"
               << setw(8) << employees[i].lastName << setw(6) << "|"
               << setw(4) << employees[i].SSN << setw(5) << "|"
               << setw(8) << employees[i].department << setw(8) << "|"
               << setw(10) << "Programmer" << "|"
               << setw(6) << employees[i].salary << setw(3) << "|" << endl;
       }
   }
   for (int i = 0; i < 74; i++) {
       cout << "-";
   }
   cout << endl;
}

---------------------------------------------------------------

Output


EMPLOYEE DETAILS:-
--------------------------------------------------------------------------
| FirstName    | LastName    | SSN    | Department    | Role     | Salary |
--------------------------------------------------------------------------
|    jhitc     |   jviqk     | 10    |   Sales       | Manager | 45041 |
|    tmufr     |   dlipg     | 11    |   Sales       |Programmer| 63467 |
|    mbupr     |   wmitw     | 12    |Accounts       |Programmer| 51334 |
|    wnuwq     |   xtojw     | 13    |   Sales       | Manager | 51499 |
|    spagj     |   dgeqd     | 14    |   Sales       |Director | 64169 |
--------------------------------------------------------------------------

Average Salary = 55102

PROGRAMMER DETAILS:-
--------------------------------------------------------------------------
| FirstName    | LastName    | SSN    | Department    | Role     | Salary |
--------------------------------------------------------------------------
|    tmufr     |   dlipg     | 11    |   Sales       |Programmer| 63467 |
|    mbupr     |   wmitw     | 12    |Accounts       |Programmer| 51334 |
--------------------------------------------------------------------------


Related Solutions

When it comes to dynamic memory allocation and delete[] pointer, what does it mean to say...
When it comes to dynamic memory allocation and delete[] pointer, what does it mean to say that, CRT detected that the application wrote to memory after end of heap buffer? I tried to duplicate the error by deleting the pointer address twice and it didn't produce the same error code so that's not it. What might cause this problem?
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
Solve this problem using pointer variables for parameter passing, where appropriate and pointer arithmetic when working...
Solve this problem using pointer variables for parameter passing, where appropriate and pointer arithmetic when working with arrays Please use the "C" program toProduce the EXACT output shown at the end of the document. 1.Generate a graph that compares, on a month-by-month basis, the monthly rainfall for Kamloops for the first half of 2018 (i.e. Jan – June) versus the normal (30 year average) rainfall for Kamloops for the same months. In main( ), create the 2 data arrays using...
In the following keypad notation Use a class and dynamic allocation of a pointer variable to...
In the following keypad notation Use a class and dynamic allocation of a pointer variable to enter the digit code of the following text input MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY Use an inherited class using pointer variable to output the phone number from the following text input 1-800-COMCAST 1-800-VERIZON 1-800-BANCORP 1-800-MYKOHLS 1-800-JCPENNY Write C++ code and pseudocode in a doc file A computer key board has defect (like speech defect in humans) in reading for ‘p’ /’P’ as...
In the following keypad notation Use a class and dynamic allocation of a pointer variable to...
In the following keypad notation Use a class and dynamic allocation of a pointer variable to enter the digit code of the following text input MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY Use an inherited class using pointer variable to output the phone number from the following text input 1-800-COMCAST 1-800-VERIZON 1-800-BANCORP 1-800-MYKOHLS 1-800-JCPENNY Write C++ code and pseudocode in a doc file A computer key board has defect (like speech defect in humans) in reading for ‘p’ /’P’ as...
Discuss what we mean by pointer arithmetic and give some examples. Discuss the relationship between passing...
Discuss what we mean by pointer arithmetic and give some examples. Discuss the relationship between passing arrays to a function and using pointers to pass an array to a function.
What function is used to deallocate dynamic memory when it is no longer required by your...
What function is used to deallocate dynamic memory when it is no longer required by your program? In your answer give the name of the function and its syntax, and how the C runtime system knows how much memory to deallocate. Give an appropriate example showing typical usage of this function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT