forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36-student_struct.cpp
60 lines (50 loc) · 1.08 KB
/
36-student_struct.cpp
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
#include <iostream>
#include <string>
#include <vector>
struct Student
{
std::string student_id{};
std::vector<int> grades{};
};
/* compute_gpa(s)
Given a student S, compute and return the GPA (average of all grades)
of S. */
float compute_gpa(Student const &s)
{
float avg{0};
for (int g : s.grades)
{
avg += g;
}
avg /= s.grades.size();
return avg;
}
/* print_student(s)
Given a student S, print the ID, list of grades
and GPA of S (using the compute_gpa function above to compute
the GPA).
*/
void print_student(Student const &s)
{
std::cout << "Student (" << s.student_id << "): ";
std::cout << "Grades are ";
for (auto g : s.grades)
{
std::cout << g << " ";
}
std::cout << "- GPA is " << compute_gpa(s);
std::cout << std::endl;
}
int main()
{
Student A{};
A.student_id = "V00999999";
A.grades.push_back(84);
A.grades.push_back(100);
Student B{"V00123456", {90, 70, 80}};
Student C{"V00666", {}};
print_student(A);
print_student(B);
print_student(C);
return 0;
}