cppintermediate15 minutes

Employee Performance Tracker

Build a C++ program to manage and analyze employee performance data by storing, updating, and calculating key statistics from the data.

Challenge prompt

Create a C++ program that enables storing performance scores of multiple employees over several months. Your program should allow the user to input employee names, their monthly performance scores, and then calculate and display the average performance for each employee. Additionally, identify the employee with the highest average score.

Guidance

  • Use structs or classes to represent an employee with fields for name and a vector/array of performance scores.
  • Provide functions to input employee data, calculate average scores, and find the top performer.
  • Handle dynamic input sizes, meaning the number of employees and the number of months can vary based on user input.

Hints

  • Consider using std::vector for flexibility in storing monthly scores.
  • A loop can be used to sum up the scores before dividing by the number of months for the average.
  • Keep track of the highest average score and corresponding employee as you calculate averages.

Starter code

#include <iostream>
#include <vector>
#include <string>

struct Employee {
    std::string name;
    std::vector<int> monthlyScores;
};

// Function prototypes
void inputEmployees(std::vector<Employee>& employees, int employeeCount, int months);
double calculateAverage(const Employee& emp);
int findTopPerformer(const std::vector<Employee>& employees);

int main() {
    int employeeCount, months;
    std::cout << "Enter number of employees: ";
    std::cin >> employeeCount;
    std::cout << "Enter number of months: ";
    std::cin >> months;

    std::vector<Employee> employees(employeeCount);
    inputEmployees(employees, employeeCount, months);

    for (const auto& emp : employees) {
        std::cout << emp.name << "'s average score: " << calculateAverage(emp) << "\n";
    }

    int topIndex = findTopPerformer(employees);
    std::cout << "Top performer: " << employees[topIndex].name << " with average score " << calculateAverage(employees[topIndex]) << "\n";

    return 0;
}

Expected output

Enter number of employees: 2 Enter number of months: 3 Enter name for employee 1: Alice Enter 3 monthly scores for Alice: 80 85 90 Enter name for employee 2: Bob Enter 3 monthly scores for Bob: 90 88 91 Alice's average score: 85 Bob's average score: 89.6667 Top performer: Bob with average score 89.6667

Core concepts

structsvectorsloopsfunctionsbasic input/output

Challenge a Friend

Send this duel to someone else and see if they can solve it.