C/C++

C++: Средний балл успеваемости каждого студента и средний балл всей группы отдельно по каждой дисциплине.

Группа из 15 студентов сдала три экзамена (О1, О2, О3 у каждого). Рассчитать и вывести средний балл успеваемости каждого студента и средний балл всей группы отдельно по каждой дисциплине.
Помогите пожалуйста.
Простое задание: создай массив и внеси туда данные о студентах. Потом в цикле посчитай средний бал для каюдого студента. А потом посчитай средний бал по всем студентам.
МБ
Михаил Бут-Гусаим
953
Лучший ответ
#include <iostream>
#include <iomanip>
#include <random>
#include <cstdlib>
using namespace std;
template<typename T>
T integer(istream& inp, const char* msg) {
cout << msg;
T value{};
inp >> value;
inp.ignore(cin.rdbuf()->in_avail());
return value;
}
struct Student {
unsigned short a : 3;
unsigned short b : 3;
unsigned short c : 3;
Student() : a(0), b(0), c(0) {}
double average()const {
return (a + b + c) / 3.0;
}
friend istream& operator>>(istream& inp, Student& s) {
s.a = integer<unsigned short>(inp, "Q1: ");
s.b = integer<unsigned short>(inp, "Q2: ");
s.c = integer<unsigned short>(inp, "Q3: ");
return inp;
}
};
int main() {
vector<Student> students(15);
auto n = 0;
for (auto& student : students) {
cout << " Student #" << ++n << '\n';
cin >> student;
}
cout.setf(ios::fixed);
cout.precision(2);
n = 0;
puts(" Average student: ");
for (auto& student : students) {
cout << "Student #" << ++n << ' ' << student.average() << '\n';
}
auto q1 = 0.0;
auto q2 = 0.0;
auto q3 = 0.0;
for (auto& student : students) {
q1 += student.a;
q2 += student.b;
q3 += student.c;
}
puts(" Average discipline: ");
cout << "Q1: " << q1 / students.size() << '\n';
cout << "Q2: " << q2 / students.size() << '\n';
cout << "Q3: " << q3 / students.size() << '\n';
system("pause > nul");
}