Problem overview
A whole team competes now, stored in three parallel vectors: names, productivities and salaries. Index i in all three describes the same employee. The program finds the employee with the highest productivity, and if several share it, the one listed first wins. It then prints the winner's name and a bonus of 15% of their salary, rounded down.
With the starter data, productivities are 83, 133, 97, 150 and 120, so Alice Chen (150) wins and her bonus is 250000 * 15 / 100 = 37500:
Alice Chen
Bonus: $37500
Earlier versions handled two and three employees with if chains. A chain needs a new branch for every employee, so with five it would be unwieldy. A loop handles any number of employees with the same code.
Approach: remember the index of the best so far
The technique is the classic "track the best" scan. Assume the first employee is the best, then visit the others one at a time. Whenever someone beats the current best, replace it.
What to remember matters. The variable stores the index of the best employee, not their productivity. One index is enough to reach the name, the productivity and the salary, because the three vectors line up. Storing the productivity alone would find the highest number but lose track of whose it is.
The tie rule fixes the comparison: an employee replaces the current best only if their productivity is strictly greater. With >=, a later employee with the same productivity would take over, and the first listed would no longer win.
Step by step
- Start
bestIndexat 0. Employee 0 is the best employee known before anyone else has been looked at. - Loop from index 1. Write a
forloop whereistarts at 1 and runs whilei < (int)names.size(). Employee 0 is already the candidate, so there is no need to compare them with themselves. The cast(int)is explained below. - Compare inside the loop. If
productivities[i]is strictly greater thanproductivities[bestIndex], setbestIndextoi. - Compute the bonus after the loop:
salaries[bestIndex] * 15 / 100. Integer division drops the fraction, which is the required rounding down. - Print the name
names[bestIndex]on its own line. - Print the bonus line as
"Bonus: $"followed bybonus.coutprints anintas it is.
The solution in C++
vector<string> names = {"John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"};
vector<int> productivities = {83, 133, 97, 150, 120};
vector<int> salaries = {135000, 335000, 210000, 250000, 180000};
int bestIndex = 0;
for (int i = 1; i < (int)names.size(); i++) {
if (productivities[i] > productivities[bestIndex]) {
bestIndex = i;
}
}
int bonus = salaries[bestIndex] * 15 / 100;
cout << names[bestIndex] << endl;
cout << "Bonus: $" << bonus << endl;
Details worth a closer look:
- The
(int)cast.names.size()returns an unsigned type,size_t. Comparing a signedintsuch asiwith an unsigned value triggers a compiler warning, and negative numbers would be converted to huge unsigned ones. Casting the size tointkeeps both sides signed. - Indexing with
[].productivities[i]does not check the index. An index outside the vector is undefined behaviour, so the loop bound must stay below the size. - Parallel vectors. Nothing in the language ties the three vectors together. The program relies on them having the same length and the same order.
Complexity
The loop visits each employee once, so the time is O(n) for n employees. The program stores a couple of integers beyond the input, so the extra space is O(1).
Edge cases and common mistakes
- Ties. With names X, Y and Z, productivities 5, 9 and 9, and salaries 100, 200 and 300, the loop sets
bestIndexto 1 at Y and does not move it at Z. The output is Y with a bonus of 30. Using>=would choose Z. - A single employee. The loop starts at 1 and the size is 1, so the body never runs and employee 0 is the answer. One employee named Solo with a salary of 1000 prints a bonus of 150.
- An empty vector.
names[0]would be out of range. The lesson's data always has employees, but a general version of this code would need to checknames.empty()first. - Storing the productivity instead of the index. The program would know the highest value but not whose name and salary to print.
- The unsigned size trap. Writing
i <= names.size() - 1breaks for an empty vector, becausesize() - 1wraps around to 18446744073709551615 on a 64-bit system, while(int)names.size() - 1is -1.
Testing
| Input | Output |
|---|---|
| the starter data | Alice Chen, then Bonus: $37500 |
| X, Y, Z with productivities 5, 9, 9 and salaries 100, 200, 300 | Y, then Bonus: $30 |
| Solo with productivity 1 and salary 1000 | Solo, then Bonus: $150 |
