cppintermediate10 minutes

C++ Student Grade Management System

Build a mini-project that manages student grades by storing names and scores, calculating averages, and allowing queries for top students.

Challenge prompt

Create a C++ program that implements a simple Student Grade Management System. Your program should allow adding multiple students with their names and a list of their scores in different subjects. Implement functionality to calculate each student's average score and find the top student(s) with the highest average score. The program should support the following operations: 1) Add a student with scores, 2) Calculate and display the average score for each student, 3) Display the student(s) with the highest average score. Use appropriate data structures to store student data efficiently.

Guidance

  • Use a struct or class to represent a student with a name and a vector of scores.
  • Keep all student records in a vector or map for easy iteration and lookup.
  • Write separate functions for adding students, calculating averages, and finding top scorers.

Hints

  • Consider using std::vector to store multiple scores per student.
  • To find the top student(s), keep track of the highest average while iterating through all students.
  • Use float or double for average scores to maintain precision.

Starter code

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

struct Student {
    std::string name;
    std::vector<int> scores;
};

class GradeManager {
    std::vector<Student> students;
public:
    void addStudent(const std::string& name, const std::vector<int>& scores) {
        // TODO: Add student to the records
    }

    void printAverages() {
        // TODO: Calculate and print average scores
    }

    void printTopStudents() {
        // TODO: Find and print the student(s) with the highest average
    }
};

int main() {
    GradeManager gm;
    // Example usage:
    gm.addStudent("Alice", {85, 90, 78});
    gm.addStudent("Bob", {92, 88, 95});
    gm.addStudent("Charlie", {70, 80, 65});
    gm.printAverages();
    gm.printTopStudents();
    return 0;
}

Expected output

Alice: Average score = 84.33 Bob: Average score = 91.67 Charlie: Average score = 71.67 Top student(s): Bob with average score = 91.67

Core concepts

Classes and StructsVectorsBasic Algorithms (averaging, max finding)

Challenge a Friend

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