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...
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++
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...
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...
Create C function for String.c and Strings.h that #include <string.h> /* substring - return a pointer...
Create C function for String.c and Strings.h that #include <string.h> /* substring - return a pointer to the substring beginning at the iPos-th position. * If iPos is out-of-range, return (char*)NULL */ char* substring(char* str, int iPos); /* charPosition - return the position of the first occurance of c in str. * If c not present, return -1 */ int charPosition(char* str, char c);
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array using a template. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
Create a function that takes a vector of vectors as an argument. Each inner vector has...
Create a function that takes a vector of vectors as an argument. Each inner vector has 2 elements. The first element is the numerator and the second element is the denominator. Return the sum of the fractions rounded to the nearest whole number. Examples: sum_fractions({{18, 13}, {4, 5}}) ➞ 2 sum_fractions({{36, 4}, {22, 60}}) ➞ 9 sum_fractions({{11, 2}, {3, 4}, {5, 4}, {21, 11}, {12, 6}}) ➞ 11 Notes Your result should be a number not string. Code in C++...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT