Question

In: Computer Science

c++ /*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/ This is the entire...

c++

/*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/

This is the entire assignment. There are no more directions to it.

  • 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.

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
}

Solutions

Expert Solution

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:


Related Solutions

Modify the Fraction class created previously as follows. Read the entire description (Code at the bottom...
Modify the Fraction class created previously as follows. Read the entire description (Code at the bottom also using C++ on visual studios) Avoid redundant code member variables numerator & denominator only do not add any member variables Set functions set numerator set denominator set fraction with one argument set fraction with two arguments set fractions with three arguments (whole, numerator, denominator) to avoid redundancy and have error checking in one place only, call setFractions with three arguments in all other...
PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h...
PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h and answer the questions in cpp files. Array is used, not linked list. It would be nice if you could comment on the code so I can understand how you wrote it employee.h file #include <string> using namespace std; class Employee { private:    string name;    int ID, roomNumber;    string supervisorName; public:    Employee();       // constructor    void setName(string name_input);   ...
Please do not use vectors or any previously posted code Write a C++ program which reads...
Please do not use vectors or any previously posted code Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data...
For C code: Do not use any function other than getchar, scanf, and printf Q2.A) Write...
For C code: Do not use any function other than getchar, scanf, and printf Q2.A) Write a program to do the following: Read in two values into variables X   and y from the keyboard (they may be decimal values like 2.3). Read in a 3rd value (which is really small < 1.0 ) into variable E. Using a loop find out the value of N in the below expression. i.e. Each iteration of the loop reduced the value of Y...
// If you modify any of the given code, the return types, or the parameters, you...
// If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // You are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of employees struct employeeList {    struct employee* employee;    struct employeeList* next; } *list = NULL;              ...
Please analyze and create a starter code in Python using spaCy, that can demonstrate practical use...
Please analyze and create a starter code in Python using spaCy, that can demonstrate practical use of the package. Also give a brief explanation of what the purpose of the spaCy package is. Please use 3 different methods such as a loop statement, if statement, tuples, lists etc. Please provide detailed comments on each line of code. Please explain the purpose of the method used. Please write and run your code example.
C Programming Please modify this code to display the "guess" value from the calculation and the...
C Programming Please modify this code to display the "guess" value from the calculation and the "iterations" it took to complete. Please also add the input to the program. Input: Program will prompt the user to provide the desired decimal-place accuracy, in the range of [1-15], inclusive. User prompt must clearly indicate what is a valid input. User input must be validated - ensuring that the value entered is an integer in the above range. Sample output for one perfect...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Code an Entire Piston Design in MATLAB or C with the given input parameters are Crank...
Code an Entire Piston Design in MATLAB or C with the given input parameters are Crank Length , crank radius, crank angle , maximum explosion pressure (Pmax) , bore , stroke Assume any for necessary values to get the output The output should include Piston clearance Top Skirt bottom Skirt crown thickness skirt thickness piston ring dimensions gudegeon pin dimensions
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT