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
iin 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;
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
