Question

In: Computer Science

The goal is to Create an array of struct “employee” Fill the array with information read...

The goal is to

  • Create an array of struct “employee”
  • Fill the array with information read from standard input using C++ style I/O
  • Shuffle the array
  • Select 5 employees from the shuffled array
  • Sort the shuffled array of employees by the alphabetical order of their last Name
  • Print this array using C++ style I/O

Random Number Seeding

We will make use of the random_shuffle() function from the standard algorithm library.

You can use srand() function provided in with a seed value so that we get random yet reproducible results. srand() seeds the pseudo random number generator for the rand() function. For a seed value to call as argument of the srand() function, you can use time(0) as seed ( defined in ). The time() function returns time_t value, which is the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e. the current unix timestamp). Since value of seed changes with time, every time a new set of random number is generated.

Employee list construction

An employee has last name, first name, birth year and hourly wage information stored in the struct. All of this information should be taken from standard input using c++ console I/O for all the employees in the array. You should print prompt messages to ask user for information on each of the field in the struct.

Now we turn our attention to random_shuffle(). This function requires three arguments: a pointer to the beginning of your array, a pointer to 1 past the end of the array, and a pointer to the function. The function will be myrandom which I have provided you. Usage is very simple, and there are many resources on how to call this function on the Internet.

Once you have your shuffled array of employees, we want to select 5 employees from the original array of employees. Again, create this however you see fit, but please use the first 5 employees in your array.

A C++ array initializer could be helpful here. For example, consider the following code:

int arr[5] = {1,2,3,4,5};

This C++ syntax allows us to explicitly define an array as it is created.

Once you’ve built an array of 5 employees, we want to sort these employees by their last Name. Notice the function headers at the top of the file:

bool name_order(const employee& lhs, const employee& rhs);

Specifically, the last function is what we need for sorting.

This is because Employee struct don’t have a natural ordering like letters in the alphabet or numbers. We need to define one. That is the job of name_order(): it takes in 2 const employee references and returns true if lhs < rhs (left-hand side, right-hand side) and false otherwise. Notice that in C++ we have a dedicated bool type we can use to represent truthiness. C++ string library has less than “<” operator overloaded so you can simply compare two strings like string1 < string2.

Implement this function and pass the name of the function as the third argument to the sort() function. The first two arguments are a pointer to the beginning of your array and a pointer to 1 past the end.

C++ I/O and iomanip

Finally, print out the sorted array using range based for loop( see lecture), C++ I/O (cout <<) and some basic I/O manipulators. I/O manipulators are functions which alter an output stream to produce specific types of formatting. For example, the setw() function linked in the resources allows you to specify the absolute width of what is printed out. For example, if I use the following code:

cout << setw(5) << “dog”;

The result would be the string “dog“ printed to the terminal. Notice the 2 spaces at the end that pad the length to 5 characters.

You should set the width and make each line printed right aligned.

When you print out the hourly wages, you should print it as double value with “fixed”, “showpoint” and “setprecision(n)” where it prints the double value with n places after the decimal point.

Sample Output

         Doe, Jane

         1990

         24.68

         Smith, John

        1989

         25.50

Starter code:

#include <iostream>#include <iomanip>#include <algorithm>#include <sstream>#include <string>#include <cstdlib>using namespace std;typedef struct Employee{string lastName;string firstName;int birthYear;double hourlyWage;}employee;bool name_order(const employee& lhs, const employee& rhs);int myrandom (int i) { return rand()%i;}int main(int argc, char const *argv[]) { // IMPLEMENT as instructed below /*This is to seed the random generator */ srand(unsigned (time(0))); /*Create an array of 10 employees and fill information from standard input with prompt messages*/ /*After the array is created and initialzed we call random_shuffle() see the *notes to determine the parameters to pass in.*/ /*Build a smaller array of 5 employees from the first five cards of the array created *above*/ /*Sort the new array. Links to how to call this function is in the specs *provided*/ /*Now print the array below */ return 0;}/*This function will be passed to the sort funtion. Hints on how to implement* this is in the specifications document.*/bool name_order(const employee& lhs, const employee& rhs) { // IMPLEMENT}

Solutions

Expert Solution

Code for your Problem is given below alongwith the screenshot of output. As you have asked the output is right aligned acoording to your sample output. The code has code comments to explain the each necessary steps in program. A brief explanation is also given about the program and functions used in program.If need any further clarification please ask in comments.

########################################################################3

EXPLANATION--->>>>

random_shuffle() function

random_shuffle function is used to randomly arrange elements. In this program we are using it to randomly arrange elements of Employee array. random_shuffle function uses rand() function for its execution. Thats why we are creating a function named myrandom() which is creating random number using rand(). we are passing this function as third parameter in random_shuffle().

sort() function

we are uysing inbuilt sort function to sort the employee array. We are passing name_order() function as third parameter to this function . This function sorts the array according to the lastname of employee.

setw() function

setw() function is used to set the the width of steam.By using it we can format all the outputs in a particular width

setprecision() function with fixed

setprecision() is used to define number of digits in output . when used with fixed the set precision sets the number of decimal digits to be equal to the argument passed in set precision. here in our program we have used setprecisio(2) along with fixed , so now the output will have two decimal digits.

showpoint

showpoint is used to format the output such that if any decimal number has no decimal port, it will add 0 at that place. As in our program we have used setprecision(2) and showpoint then if a number is 78.3 then it will convert to 78.30 .

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

CODE-->>

#include <iostream>
#include <iomanip>
#include<algorithm>
#include <sstream>
#include <cstdlib>

using namespace std;
//creating structore for employee using struct keywords
//using typedef
typedef struct Employee
{
    string lastName;
    string firstName;
    int birthYear;
    double hourlyWage;
}employee;
//function to be used for third parameter in sort(), sorts according to last name
bool name_order(const employee& lhs,const employee& rhs)
{
    //if lhs.lastname comes first is natural ordering sends true otherwise false
    if(lhs.lastName<rhs.lastName)
        return true;
    else
        return false;
}
//function to act as third parameter in random_shuffle function
int myrandom(int i)
{
    return rand()%i;
}

//main function
int main(int argc,char const *argv[])
{
   //seeding the srand with time
    srand(unsigned(time(0)));
    //creating array of employee
    employee detail[10];
    cout<<"Enter details of Employees";
   //taking input for the employee
    for(int i=0;i<10;i++)
    {
        cout<<"\n\n------Details of Employee "<<i+1<<" ------";
        cout<<"\nEnter the First name of Employee : ";
        //input first name
        cin>>detail[i].firstName;
        cout<<"Enter the Last name of Employee : ";
       //input last name
        cin>>detail[i].lastName;
        cout<<"Enter the Birth Year of Employee : ";
        cin>>detail[i].birthYear;
        cout<<"Enter the hourly Wage of Employee : ";
        //input hourly wage
        cin>>detail[i].hourlyWage;
    }
   //calling random_shuffle with myrandom as thrid parameter so that it can shiffle array with use of rand for random no generation
    random_shuffle(detail,detail+10,myrandom);
   // creating new  array with first 5 employyes from shuffled array
    employee newArray[5]={detail[0],detail[1],detail[2],detail[3],detail[4]};
    //sorting the array according to last name using name_order()
    sort(newArray,newArray+5,name_order);

    cout <<"\n\nEmployee Details After Sorting are: ";
    //displaying details of employee with right alignment and precision
    for(int i=0;i<5;i++)
    {
        //display right aligned first name and last name
       cout<<"\n";
        cout<<setw(16)<<right<<newArray[i].lastName<<", "<<newArray[i].firstName;
        cout<<"\n";
        //display right aligned birthyear
        cout<<setw(16)<<right<<newArray[i].birthYear;
        cout<<"\n";
        //display hourly wage with right align and precision
        cout<<setw(16)<<setprecision(2)<<right<<showpoint<<fixed<<newArray[i].hourlyWage;
    }
    return 0;
}

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

OUTPUT-->>


Related Solutions

Using C++ language, create a program that uses a struct with array variables that will loop...
Using C++ language, create a program that uses a struct with array variables that will loop at least 3 times and get the below information: First Name Last Name Job Title Employee Number Hours Worked Hourly Wage Number of Deductions Claimed Then, determine if the person is entitled to overtime and gross pay. Afterwards, determine the tax and net pay. Output everything to the screen. Use functions wherever possible. Bonus Points: Use an input file to read in an unknown...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Develop a python program to - Create a 2D 10x10 numpy array and fill it with...
Develop a python program to - Create a 2D 10x10 numpy array and fill it with the random numbers between 1 and 9 (including 0 and 9). - Assume that you are located at (0,0) (upper-left corner) and want to move to (9,9) (lower-right corner) step by step. In each step, you can only move right or down in the array. - Develop a function to simulate random movement from (0,0) to (9,9) and return sum of all cell values...
The task is to sort structs in array. The struct is struct coor{ int x; int...
The task is to sort structs in array. The struct is struct coor{ int x; int y; int z; }; Create an array with 100 coordinates, which are randomly initiated with x between 0 and 30 and y between 5 and 60, and z between 10 and 90. Then modify the Quick Sort algorithm (QuickSort.cpp) to sort coordinated. Comparison of the coordinates should be carried on first x, then y, and z last. It means that if two points has...
To store the information about a city and its weather forecast, create a struct that holds...
To store the information about a city and its weather forecast, create a struct that holds the city's name (string), population (int), and a forecast array storing 7 string elements representing the city's weather in the next 7 days. The program will then accept user input to enter the name and population of the city, then it accepts 7 string inputs to store into the forecast array. Then the program will output the city's name, population, and forecast data. Ex:...
C++ programming To store the information about a city and its weather forecast, create a struct...
C++ programming To store the information about a city and its weather forecast, create a struct that holds the city's name (string), population (int), and a forecast array storing 7 string elements representing the city's weather in the next 7 days. The program will then accept user input to enter the name and population of the city, then it accepts 7 string inputs to store into the forecast array. Then the program will output the city's name, population, and forecast...
Describe the goal of employee relations (ER) programs, and then illustrate how an employer can create...
Describe the goal of employee relations (ER) programs, and then illustrate how an employer can create a more positive organizational culture through: (a) employee involvement strategies, (b) communicating with employees, and (c) by providing employee feedback. Identify and describe at least one strategy or program in each area (a, b, and c.).
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Code in python: Write a class that implements a struct. In your struct store Student information...
Code in python: Write a class that implements a struct. In your struct store Student information such as name, netid, and gpa. Write a function that can access a student in a list of 5 structs by netid and print their name and gpa. Show that your function works by calling it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT