In: Computer Science
c++
/*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/
This is the entire assignment. There are no more directions to it.
Random Number Seeding
We will make use of the random_shuffle() function from the standard algorithm library.
The random number generator in the C standard library is actually a pseudo-random number generator. Usually this is implemented by taking a seed value and applying some simple math to generate a seemingly random sequence of numbers. More info here. What is really interesting about these deterministic number generators is that we can reproduce a list of random numbers by providing the same seed value for each run of the program.
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.
More details here https://en.cppreference.com/w/cpp/numeric/random/srand
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.
Remember from sorting algorithms, you should have covered in CPSC 1010 or 1110, that we could order a list of integers with little problem. However, with a more complex model like a employee struct, there is more than one way in which we could order a list: last name, first name, birth year or hourly wage.
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.
See the linked resources on sort() and using the function with a plain array.
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.
For range-based loop examples, see the lecture provided in the class.
Sample Output
If I invoke your program as follows:
./lab6
one such output should be similar to following but with right alignment and sorted by last Name(The output here is not accurate since I typed it here, not copied from console:
Doe, Jane
1990
24.68
Humaira, Nushrat
1989
25.50
STARTER CODE
#include #include #include #include #include #include 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: stuCards.cpp
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<iomanip>
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*/
employee arr[10];
for(int i=0; i<10; i++){
cout<<"Employee["<<i+1<<"]..."<<endl;
cout<<"Enter employee's first name: "; cin>>arr[i].firstName;
cout<<"Enter employee's last name: "; cin>>arr[i].lastName;
cout<<"Enter employee's birth year: "; cin>>arr[i].birthYear;
cout<<"Enter employee's hourly wages: "; cin>>arr[i].hourlyWage;
}
/*After the array is created and initialzed we call random_shuffle() see the
*notes to determine the parameters to pass in.*/
random_shuffle(arr, arr+10, myrandom);
/*Build a smaller array of 5 employees from the first five cards of the array created
*above*/
employee shuffledEmp[5];
for(int i=0; i<5; i++)
shuffledEmp[i] = arr[i];
/*Sort the new array. Links to how to call this function is in the specs
*provided*/
sort(shuffledEmp, shuffledEmp+5, name_order);
/*Now print the array below */
for(int i=0; i<5; i++){
cout<< right << setw(30) << (shuffledEmp[i].lastName + ", " + shuffledEmp[i].firstName) <<endl;
cout<< right << setw(30) << shuffledEmp[i].birthYear<<endl;
cout<< right << setw(30) << fixed << setprecision(2)<< shuffledEmp[i].hourlyWage<<endl;
}
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) {
return lhs.lastName < rhs.lastName;
}
Output snippet: