Question

In: Computer Science

8.20 Person Pointer Function Make a function that will accept a pointer to a vector of...

8.20 Person Pointer Function Make a function that will accept a pointer to a vector of Person pointers as a parameter. Return a pointer to the Person with the highest salary. The function must be signatured like: Person* getBestPaid(vector*); The class definition is: class Person { public: double salary; string name; }; You may include code in your main() to test, but the tests in this assignment will ensure your code is correct. You may assume there will ways be at least 1 element in the vector for the purpose of this exercise.

Solutions

Expert Solution

//highsal.cpp


#include <iostream>
#include <string>
#include <vector>
using namespace std;

// class definition
class Person
{
public:
    double salary;
    string name;
    // constructor for initializing the data
    Person(double sal, string n)
    {
        salary = sal;
        name = n;
    }
};

// function to find best paid person
Person *getBestPaid(vector<Person *> persons)
{
    // intial best persion paid it at 0 index
    Person *p = persons[0];
    // iterating through persons
    for (auto &per : persons)
    {
        // comparing salary
        // if greater then updating person
        if (per->salary > p->salary)
            p = per;
    }
    return p;
}
int main()
{
    // vector if Person pointer
    vector<Person *> persons;
    // added few test data
    persons.push_back(new Person(2132, "rakesh"));
    persons.push_back(new Person(64578, "amit"));
    persons.push_back(new Person(6734, "Sona"));
    persons.push_back(new Person(878734, "Kranti"));
    persons.push_back(new Person(3442, "Somya"));
    persons.push_back(new Person(2333, "Rohit"));

    // calling the function and printig the result
    Person *bestPaidPerson = getBestPaid(persons);
    cout << "Best Paid person" << endl
         << "Name: " << bestPaidPerson->name << "\tSalary: " << bestPaidPerson->salary << endl;
}

// OUTPUT

Please do let me know if u have any concern...


Related Solutions

Write a function that will accept one integer matrix E and one column vector F by...
Write a function that will accept one integer matrix E and one column vector F by reference parameters, and one integer I as a value parameter. The function will return an integer v, which is the i-th coefficient of row vector denoted by E*F (multiplication of E and F). For example, if V = E*F, the function will return v, which is equal to V[i]. Explain the time complexity of this function inside of the function code as a comment....
Q. Make a vector of weights in order from the youngest person to the oldest. What...
Q. Make a vector of weights in order from the youngest person to the oldest. What is the code here? I should get a code in R program. firstName gender age height weight bmi overWt 1 Tom m 77 70 175 25.16238799 TRUE 2 May f 33 64 125 21.50106395 FALSE 3 Joe m 79 73 185 24.45884214 FALSE 4 Bob m 47 67 156 24.4841414 FALSE 5 Sue f 27 64 105 18.06089372 FALSE 6 Liz f 33 68...
Send an element from 2d array to a function using pointer to pointer. c++ I am...
Send an element from 2d array to a function using pointer to pointer. c++ I am having an issue with being able to send specific elements to the swap function. #include <iostream> using namespace std; void swap(int **a, int **b){    int temp = **a;    **a=**b;    **b=temp; } void selSort(int **arr, int d){    for(int i =0; i<d-1; i++){        int min = *(*arr+i);        int minin = i;        for(int j =i+1; j<d; j++){...
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
Suppose a function receives a pointer as an argument. Explain how this function is declared within...
Suppose a function receives a pointer as an argument. Explain how this function is declared within its calling function. In particular, explain how the data type of the pointer argument is represented. C++
Write a function that will accept a list of numbers and an integer (n). The function...
Write a function that will accept a list of numbers and an integer (n). The function should return a list containing every nth item from the input list, always starting with the first item in the list. The original list should not be modified. For example, if the function is passed the list [8, 3, 19, 26, 32, 12, 3, 7, 21, 16] and the integer 3, it will return the list [8, 26, 3, 16] If the function is...
Implement a function named printRange that, given the pointer to the root of a BST, a...
Implement a function named printRange that, given the pointer to the root of a BST, a low key value, and a high key value, prints in sorted order all records whose key values fall between the two given keys (inclusive). Function printRange should visit as few nodes in the BST as possible. Here is the start code (in Java 8) for this problem. Input Format Three lines. The first line includes the number of keys to be put in the...
Write a function to concatenate two strings using pointer notation
Write a function to concatenate two strings using pointer notation
1) Write a function that receives a pointer to an array along with the size of...
1) Write a function that receives a pointer to an array along with the size of the array, it returns the largest number in the array. 2) Write a function that receives a string and returns the length of it.
pUC19 is a cloning vector, but cannot function as an expression vector. a) Explain the difference...
pUC19 is a cloning vector, but cannot function as an expression vector. a) Explain the difference in requirements for cloning and expression vectors. b) For the purpose of expressing a eukaryotic protein in bacterial cells, will you rather use a clone from a genomic or a cDNA library? Motivate. c) Assume you have created a genomic and cDNA library for the same cell type. You then screen both libraries with the same probe. You observe a signal from some of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT