# STL Assignment

Created by Abhishek Sharma about 7 months ago

0

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

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

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

}

``````for (auto& pupil : pupils) {
if (pupil.getId() == id) {
string course;
cout << "Enter the course name: ";
cin >> course;
cout << "Enter the grade for " << course << ": ";
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; 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;

cout << "Enter the new grade for " << course << ": ";
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; }

}

}

``````return 0;