-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
108 lines (98 loc) · 3.24 KB
/
main.cpp
File metadata and controls
108 lines (98 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const int NUM_SUBJECTS = 3;
struct Student {
string name;
int age;
int marks[NUM_SUBJECTS];
float averageMarks;
};
void calculateAverageMarks(Student &student) {
int total = 0;
for (int i = 0; i < NUM_SUBJECTS; ++i) {
total += student.marks[i];
}
student.averageMarks = static_cast<float>(total) / NUM_SUBJECTS;
}
void addStudent(vector<Student> &students) {
Student newStudent;
cout << "Enter name: ";
cin >> newStudent.name;
cout << "Enter age: ";
cin >> newStudent.age;
cout << "Enter marks for " << NUM_SUBJECTS << " subjects: ";
for (int i = 0; i < NUM_SUBJECTS; ++i) {
cin >> newStudent.marks[i];
}
calculateAverageMarks(newStudent);
students.push_back(newStudent);
}
void displayStudents(const vector<Student> &students) {
for (const auto &student : students) {
cout << "Name: " << student.name << ", Age: " << student.age << ", Average Marks: " << student.averageMarks << endl;
}
}
void sortStudentsByAverageMarks(vector<Student> &students) {
sort(students.begin(), students.end(), {
return a.averageMarks > b.averageMarks;
});
}
void findHighestAndLowestAverage(const vector<Student> &students) {
if (students.empty()) {
cout << "No student records available." << endl;
return;
}
auto maxStudent = max_element(students.begin(), students.end(), {
return a.averageMarks < b.averageMarks;
});
auto minStudent = min_element(students.begin(), students.end(), {
return a.averageMarks < b.averageMarks;
});
cout << "Highest Average Marks: " << maxStudent->name << " with " << maxStudent->averageMarks << endl;
cout << "Lowest Average Marks: " << minStudent->name << " with " << minStudent->averageMarks << endl;
}
void calculateClassAverage(const vector<Student> &students) {
if (students.empty()) {
cout << "No student records available." << endl;
return;
}
float totalAverage = 0;
for (const auto &student : students) {
totalAverage += student.averageMarks;
}
cout << "Class Average Marks: " << totalAverage / students.size() << endl;
}
int main() {
vector<Student> students;
int choice;
do {
cout << "Menu:\n1. Add new student records\n2. Display all student records\n3. Sort students by average marks\n4. Display student with highest and lowest average\n5. Calculate class average\n6. Exit\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent(students);
break;
case 2:
displayStudents(students);
break;
case 3:
sortStudentsByAverageMarks(students);
break;
case 4:
findHighestAndLowestAverage(students);
break;
case 5:
calculateClassAverage(students);
break;
case 6:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 6);
return 0;
}