Start learning free

LESSON 182 OF 667 · LOOPING OVER ARRAY INDICES

Employee Of The Year V4 in C++: Finding the Best with a for Loop

Practice Read the full explanation

A whole team is competing. Use a for loop over the vectors to find the most productive employee, then print their 15% bonus.

You start with:

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};

Build:

Alice Chen
Bonus: $37500

Rules:

  • Index i in all three vectors refers to the same employee.
  • The winner is whichever employee has the highest productivity. If two employees share the highest productivity, the one listed first wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Start with int bestIndex = 0 and loop over the other indices with for (int i = 1; i < (int)names.size(); i++). Inside the loop, update the best index only when productivities[i] > productivities[bestIndex]: a strict comparison keeps the first employee on a tie.

Dividing two int values drops the fraction, so salaries[bestIndex] * 15 / 100 is already rounded down: int bonus = salaries[bestIndex] * 15 / 100;

Chain the text and the number with <<: cout << "Bonus: $" << bonus << endl;

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;

STEP 1 OF 6
    Ctrl + Enter to check

    C++ · LOOPING OVER ARRAY INDICES

    Employee Of The Year V4 in C++, explained

    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

    1. Start bestIndex at 0. Employee 0 is the best employee known before anyone else has been looked at.
    2. Loop from index 1. Write a for loop where i starts at 1 and runs while i < (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.
    3. Compare inside the loop. If productivities[i] is strictly greater than productivities[bestIndex], set bestIndex to i.
    4. Compute the bonus after the loop: salaries[bestIndex] * 15 / 100. Integer division drops the fraction, which is the required rounding down.
    5. Print the name names[bestIndex] on its own line.
    6. Print the bonus line as "Bonus: $" followed by bonus. cout prints an int as 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 signed int such as i with an unsigned value triggers a compiler warning, and negative numbers would be converted to huge unsigned ones. Casting the size to int keeps 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 bestIndex to 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 check names.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() - 1 breaks for an empty vector, because size() - 1 wraps around to 18446744073709551615 on a 64-bit system, while (int)names.size() - 1 is -1.

    Testing

    InputOutput
    the starter dataAlice Chen, then Bonus: $37500
    X, Y, Z with productivities 5, 9, 9 and salaries 100, 200, 300Y, then Bonus: $30
    Solo with productivity 1 and salary 1000Solo, then Bonus: $150
    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free