In: Computer Science
The goal is to
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}
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-->>