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

50.1K

Verified Solution

Question

Programming

The goal is to

  • Create an array of struct “employee”
  • Fill the array with information read from standard input usingC++ style I/O
  • Shuffle the array
  • Select 5 employees from the shuffled array
  • Sort the shuffled array of employees by the alphabetical orderof 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 aseed 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, youcan use time(0) as seed ( defined in ). The time() function returnstime_t value, which is the number of seconds since 00:00 hours, Jan1, 1970 UTC (i.e. the current unix timestamp). Since value of seedchanges with time, every time a new set of random number isgenerated.

Employee list construction

An employee has last name, first name, birth year and hourlywage information stored in the struct. All of this informationshould be taken from standard input using c++ console I/O for allthe employees in the array. You should print prompt messages to askuser 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 beginningof your array, a pointer to 1 past the end of thearray, and a pointer to the function. The function will be myrandomwhich I have provided you. Usage is very simple, and there are manyresources on how to call this function on the Internet.

Once you have your shuffled array of employees, we want toselect 5 employees from the original array of employees. Again,create this however you see fit, but please use the first 5employees 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 iscreated.

Once you’ve built an array of 5 employees, we want tosort these employees by their lastName. Notice the function headers at the top of thefile:

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

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

This is because Employee struct don’t have a naturalordering like letters in the alphabet or numbers. We needto define one. That is the job of name_order(): it takes in 2 constemployee references and returns true if lhs falseotherwise. Notice that in C++ we have a dedicatedbool type we can use to represent truthiness. C++string library has less than “<” operator overloaded so you cansimply compare two strings like string1 < string2.

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

C++ I/O and iomanip

Finally, print out the sorted array using range basedfor loop( see lecture), C++ I/O (cout <<) and somebasic I/O manipulators. I/O manipulatorsare functions which alter an output stream to produce specifictypes of formatting. For example, the setw() function linked in theresources allows you to specify the absolute width of what isprinted 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 5characters.

You should set the width and make each line printed rightaligned.

When you print out the hourly wages, you should print it asdouble value with “fixed”, “showpoint” and “setprecision(n)” whereit prints the double value with n places after the decimalpoint.

Sample Output

         Doe,Jane

        1990

        24.68

         Smith,John

       1989

        25.50

Starter code:

#include #include #include#include #include#include using namespace std;typedefstruct Employee{string lastName;string firstName;intbirthYear;double hourlyWage;}employee;bool name_order(constemployee& 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 randomgenerator */ srand(unsigned (time(0))); /*Create an array of 10employees and fill information from standard input with promptmessages*/ /*After the array is created and initialzed we callrandom_shuffle() see the *notes to determine the parameters to passin.*/ /*Build a smaller array of 5 employees from the first fivecards of the array created *above*/ /*Sort the new array. Links tohow to call this function is in the specs *provided*/ /*Now printthe array below */ return 0;}/*This function will be passed to thesort funtion. Hints on how to implement* this is in thespecifications document.*/bool name_order(const employee& lhs,const employee& rhs) { // IMPLEMENT}

Answer & Explanation Solved by verified expert
4.2 Ratings (720 Votes)
Code for your Problem is given below alongwith the screenshot ofoutput As you have asked the output is right aligned acoording toyour sample output The code has code comments to explain the eachnecessary steps in program A brief explanation is also given aboutthe program and functions used in programIf need any furtherclarification please ask in comments3EXPLANATIONrandomshufflefunctionrandomshuffle function    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students