fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Student {
  5. int id;
  6. int totalScore{0};
  7. unordered_map<int, int> grades;
  8. };
  9.  
  10. struct Assignment {
  11. int id;
  12. };
  13.  
  14. class Course {
  15. private:
  16. unordered_map<int, Student> stds;
  17. unordered_map<int, Assignment> asgns;
  18.  
  19. public:
  20. Course() {}
  21.  
  22. void addAssignment(int aId) {
  23. if (asgns.count(aId)) {
  24. cout << "Assignment " << aId << " already exists." << endl;
  25. return;
  26. }
  27. asgns.emplace(aId, Assignment{aId});
  28. }
  29.  
  30. void enrollStudent(int sId) {
  31. if (stds.count(sId)) {
  32. cout << "Student " << sId << " is already enrolled." << endl;
  33. return;
  34. }
  35. stds.emplace(sId, Student{sId});
  36. }
  37.  
  38. void addStudentGrade(int sId, int aId, int grd) {
  39. if (!stds.count(sId)) {
  40. cout << "Student " << sId << " is not enrolled." << endl;
  41. return;
  42. }
  43. if (!asgns.count(aId)) {
  44. cout << "Assignment " << aId << " does not exist." << endl;
  45. return;
  46. }
  47.  
  48. auto& std = stds.at(sId);
  49. if (std.grades.count(aId)) {
  50. std.totalScore -= std.grades[aId];
  51. }
  52. std.totalScore += grd;
  53. std.grades[aId] = grd;
  54. }
  55.  
  56. float getStudentGrade(int sId) {
  57. if (!stds.count(sId)) {
  58. cout << "Student " << sId << " is not enrolled." << endl;
  59. return -1.0f;
  60. }
  61.  
  62. auto& std = stds.at(sId);
  63. if (std.grades.empty()) {
  64. cout << "No grades available for student " << sId << "." << endl;
  65. return -1.0f;
  66. }
  67.  
  68. return static_cast<float>(std.totalScore) / std.grades.size();
  69. }
  70.  
  71. void unenrollStudent(int sId) {
  72. if (stds.count(sId)) {
  73. stds.erase(sId);
  74. }
  75. }
  76.  
  77. float getOverallAssignmentGrade(int aId) {
  78. if (!asgns.count(aId)) {
  79. cout << "Assignment " << aId << " does not exist." << endl;
  80. return -1.0f;
  81. }
  82.  
  83. int total = 0;
  84. int count = 0;
  85. for (auto const& [sId, std] : stds) {
  86. if (std.grades.count(aId)) {
  87. total += std.grades.at(aId);
  88. count++;
  89. }
  90. }
  91.  
  92. if (count == 0) {
  93. cout << "No grades submitted for assignment " << aId << "." << endl;
  94. return -1.0f;
  95. }
  96. return static_cast<float>(total) / count;
  97. }
  98.  
  99. vector<int> getUninvolvedStudents() {
  100. vector<int> result;
  101. size_t threshold = asgns.size() / 2;
  102. for (auto const& [sId, std] : stds) {
  103. if (std.grades.size() <= threshold) {
  104. result.push_back(sId);
  105. }
  106. }
  107. return result;
  108. }
  109.  
  110. int getStudentWorstAssignment(int sId) {
  111. if (!stds.count(sId)) {
  112. cout << "Student " << sId << " not found." << endl;
  113. return -1;
  114. }
  115.  
  116. auto& std = stds.at(sId);
  117. if (std.grades.empty()) {
  118. cout << "No grades for student " << sId << "." << endl;
  119. return -1;
  120. }
  121.  
  122. int worstAsgnId = -1;
  123. int minGrade = INT_MAX;
  124. for (auto const& [aId, grd] : std.grades) {
  125. if (grd < minGrade) {
  126. minGrade = grd;
  127. worstAsgnId = aId;
  128. }
  129. }
  130. return worstAsgnId;
  131. }
  132.  
  133. vector<int> getTopStudents() {
  134. vector<pair<float, int>> studentAvgs;
  135. for (auto const& [sId, std] : stds) {
  136. if (!std.grades.empty()) {
  137. float avg = static_cast<float>(std.totalScore) / std.grades.size();
  138. studentAvgs.push_back({avg, sId});
  139. }
  140. }
  141.  
  142. sort(studentAvgs.rbegin(), studentAvgs.rend());
  143.  
  144. vector<int> topStudents;
  145. int count = 0;
  146. for(const auto& p : studentAvgs) {
  147. if (count++ < 3) {
  148. topStudents.push_back(p.second);
  149. } else {
  150. break;
  151. }
  152. }
  153. return topStudents;
  154. }
  155. };
  156.  
  157. int main() {
  158. Course course;
  159.  
  160. cout << "--- Initial Setup ---" << endl;
  161. course.enrollStudent(101);
  162. course.enrollStudent(102);
  163. course.enrollStudent(103);
  164. course.enrollStudent(104);
  165. course.enrollStudent(101);
  166.  
  167. course.addAssignment(1);
  168. course.addAssignment(2);
  169. course.addAssignment(3);
  170. course.addAssignment(4);
  171. course.addAssignment(1);
  172.  
  173. cout << "\n--- Adding Grades ---" << endl;
  174. course.addStudentGrade(101, 1, 90);
  175. course.addStudentGrade(101, 2, 80);
  176. course.addStudentGrade(102, 1, 100);
  177. course.addStudentGrade(102, 2, 95);
  178. course.addStudentGrade(102, 3, 98);
  179. course.addStudentGrade(103, 1, 85);
  180. course.addStudentGrade(103, 2, 88);
  181. course.addStudentGrade(103, 4, 70);
  182. course.addStudentGrade(105, 1, 90);
  183. course.addStudentGrade(101, 5, 90);
  184.  
  185. cout << "\n--- Calculating Averages ---" << endl;
  186. cout << "Student 101 Average: " << course.getStudentGrade(101) << endl;
  187. cout << "Student 102 Average: " << course.getStudentGrade(102) << endl;
  188. cout << "Student 104 Average: " << course.getStudentGrade(104) << endl;
  189.  
  190. cout << "\n--- Part 2 Function Tests ---" << endl;
  191.  
  192. cout << "\nOverall grade for Assignment 1: " << course.getOverallAssignmentGrade(1) << endl;
  193. cout << "Overall grade for Assignment 3: " << course.getOverallAssignmentGrade(3) << endl;
  194.  
  195. cout << "\nStudent 102's worst assignment: " << course.getStudentWorstAssignment(102) << endl;
  196. cout << "Student 103's worst assignment: " << course.getStudentWorstAssignment(103) << endl;
  197.  
  198. cout << "\nUninvolved students: ";
  199. vector<int> uninvolved = course.getUninvolvedStudents();
  200. for(int id : uninvolved) cout << id << " ";
  201. cout << endl;
  202.  
  203. cout << "\nUnenrolling student 103..." << endl;
  204. course.unenrollStudent(103);
  205. cout << "Trying to get Student 103 Average: " << course.getStudentGrade(103) << endl;
  206.  
  207. cout << "\nTop students: ";
  208. vector<int> top = course.getTopStudents();
  209. for(int id : top) cout << id << " ";
  210. cout << endl;
  211.  
  212. return 0;
  213. }
  214.  
  215.  
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
--- Initial Setup ---
Student 101 is already enrolled.
Assignment 1 already exists.

--- Adding Grades ---
Student 105 is not enrolled.
Assignment 5 does not exist.

--- Calculating Averages ---
Student 101 Average: 85
Student 102 Average: 97.6667
Student 104 Average: No grades available for student 104.
-1

--- Part 2 Function Tests ---

Overall grade for Assignment 1: 91.6667
Overall grade for Assignment 3: 98

Student 102's worst assignment: 2
Student 103's worst assignment: 4

Uninvolved students: 104 101 

Unenrolling student 103...
Trying to get Student 103 Average: Student 103 is not enrolled.
-1

Top students: 102 101