codedamn

STL Assignment

Created by Abhishek Sharma about 7 months ago

0

An interactive lab from the From Novice to Expert: Mastering C++ Programming course.

2 Comments

    0

    #include <iostream> #include <vector> #include <algorithm>

    int main() { std::vector <int> var; int num; do { std::cin >> num; var.push_back(num); } while (num > 0); for (auto i : var) { std::cout << i << " "; } std::cout << std::endl; //sort vectors std::sort(var.begin(), var.end()); //print in ascending order for(auto i: var) { std::cout << i << " "; } return 0; }

    @mohamedazimal

    Mohamed Azimal

    @mohamedazimal

    0

    #include <iostream> #include <vector> #include <deque> #include <algorithm> #include <list> #include <stack> #include <set> #include <map> #include <iomanip> #include <string>

    using namespace std; class Pupil { int id; string name; map<string, int> courseGradeMap; public: Pupil(int id, string name, map<string, int> courseGradeMap) { this->id = id; this->name = name; this->courseGradeMap = courseGradeMap; } int getId() { return id; } string getName() { return name; } map<string, int> getCourseGradeMap() { return courseGradeMap; }

    };

    void addNewPupil(vector<Pupil>* pupils) { int id; string name; int courseCnt; map<string, int> courseGradeMap; cout << "Enter pupil's ID: "; cin >> id; cin.ignore(); // Clears the input buffer cout << "Enter pupil's name: "; getline(cin, name); cout << "How many courses is the pupil taking? "; cin >> courseCnt; for (int i = 0; i < courseCnt; i++) { string course; int grade; cout << "Enter the name of course " << (i + 1) << ": "; cin >> course; cout << "Enter the grade for " << course << ": "; cin >> grade; courseGradeMap[course] = grade; } pupils->push_back(Pupil(id, name, courseGradeMap)); cout << "Pupil " << name << " has been added successfully." << endl; }

    void removePupil(vector<Pupil>* pupils) { int id; bool idFoud = false; cout << "Enter pupil's ID : "; cin >> id; for (auto pupil : *pupils) { if (pupil.getId() == id) { cout << "Pupil " << pupil.getName() << " has been removed successfully." << endl; idFoud = true; } } if (!idFoud) { cout << "Pupil's ID " << id << " has not found." << endl; }

    }

    void removePupil(vector<Pupil>& pupils) { int id; cout << "Enter the ID of the pupil to remove: "; cin >> id;

    auto it = std::remove_if(pupils.begin(), pupils.end(),
        [id](const Pupil& pupil) { return pupil.getId() == id; });
    
    if (it != pupils.end()) {
        cout << "Pupil with ID " << id << " has been removed successfully." << endl;
        pupils.erase(it, pupils.end());
    }
    else {
        cout << "No pupil found with ID " << id << "." << endl;
    }
    

    }

    void addGrade(vector<Pupil>* pupils) { int id; bool idFoud = false; cout << "Enter pupil's ID : "; cin >> id; for (auto pupil : *pupils) { if (pupil.getId() == id) { string course; int grade; cout << "Enter the name of course : "; cin >> course; cout << "Enter the grade for " << course << ": "; cin >> grade; pupil.getCourseGradeMap()[course] = grade; cout << course << " has been added successfully." << endl; idFoud = true;

        }
    }
    if (!idFoud)
    {
        cout << "Pupil's ID " << id << " has not found." << endl;
    }
    

    }

    void addGrade(vector<Pupil>& pupils) { int id; cout << "Enter pupil's ID to add a grade: "; cin >> id;

    for (auto& pupil : pupils) {
        if (pupil.getId() == id) {
            string course;
            int grade;
            cout << "Enter the course name: ";
            cin >> course;
            cout << "Enter the grade for " << course << ": ";
            cin >> grade;
            pupil.getCourseGradeMap()[course] = grade;
            cout << "Grade for course " << course << " has been added successfully." << endl;
            return;
        }
    }
    cout << "No pupil found with ID " << id << "." << endl;
    

    }

    void modifyGrade(vector<Pupil>* pupils) { int id; bool idFoud = false; bool gradeFound = true; cout << "Enter pupil's ID : "; cin >> id; for (auto pupil : *pupils) { if (pupil.getId() == id) { string course; int grade; cout << "Enter the name of course : "; cin >> course; for (auto i : pupil.getCourseGradeMap()) { if (i.first == course) { cout << "Enter the grade for " << course << ": "; cin >> grade; pupil.getCourseGradeMap()[course] = grade; cout << "Pupil course " << course << " grade is successfully updated." << endl; } } if (!gradeFound) { cout << "Pupil's course " << course << " has not found." << endl; } idFoud = true; } } if (!idFoud) { cout << "Pupil's ID " << id << " has not found." << endl; }

    }

    void modifyGrade(vector<Pupil>& pupils) { int id; cout << "Enter pupil's ID to modify a grade: "; cin >> id;

    for (auto& pupil : pupils) {
        if (pupil.getId() == id) {
            string course;
            cout << "Enter the course name: ";
            cin >> course;
    
            auto& grades = pupil.getCourseGradeMap();
            if (grades.find(course) != grades.end()) {
                int newGrade;
                cout << "Enter the new grade for " << course << ": ";
                cin >> newGrade;
                grades[course] = newGrade;
                cout << "Grade for course " << course << " has been updated successfully." << endl;
            }
            else {
                cout << "Course " << course << " not found for pupil ID " << id << "." << endl;
            }
            return;
        }
    }
    cout << "No pupil found with ID " << id << "." << endl;
    

    }

    void printPupilGrades(vector<Pupil>* pupils) { int id; bool idFoud = false; cout << "Enter pupil's ID : "; cin >> id; for (auto pupil : *pupils) { if (pupil.getId() == id) { for (auto course : pupil.getCourseGradeMap()) { cout << "pupil " << pupil.getName() << " has score " << course.second << " in " << course.first << " course" << endl; } } } if (!idFoud) { cout << "Pupil's ID " << id << " has not found." << endl; }

    }

    void averageCourseGrade(vector<Pupil>* pupils) { bool courseFound = false; string course; cout << "Enter the name of course : "; cin >> course; int totalMarks = 0; int noOfStudentTakeCourse = 0; for (auto i : *pupils) { for (auto allCourse : i.getCourseGradeMap()) { if (allCourse.first == course) { totalMarks += allCourse.second; noOfStudentTakeCourse++; courseFound = true; } } } if (courseFound) { cout << "Average grade of " << course << " is : " << (totalMarks / noOfStudentTakeCourse) << endl; } else { cout << "No such " << course << " course exist." << endl; }

    }

    void addCourse(vector<Pupil>* pupils) {

    }

    int main() { vector<Pupil> pupils; bool programSwitch = true; int choice; while (programSwitch) { cout << setw(40) << left << "Welcome to Student Management System" << endl; cout << setw(40) << left << "1) Add pupil" << endl; cout << setw(40) << left << "2) Remove pupil" << endl; cout << setw(40) << left << "3) Add grade" << endl; cout << setw(40) << left << "4) Modify grade" << endl; cout << setw(40) << left << "5) Print pupil grades" << endl; cout << setw(40) << left << "6) Print average course grade" << endl; cout << "Input option: "; cin >> choice; switch (choice) { case 1: addNewPupil(&pupils); break; case 2: removePupil(&pupils); break; case 3: addGrade(&pupils); break; case 4: modifyGrade(&pupils); break; case 5: printPupilGrades(&pupils); break; case 6: averageCourseGrade(&pupils); break; } }

    return 0;
    

    }

    @tenzind

    tenzin dadhul

    @tenzind

Your comment