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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#include <iostream>
// Source: https://github.com/triffon/oop-2021-22/blob/main/practicum/6/week1/%D0%A1%D1%82%D1%80%D1%83%D0%BA%D1%82%D1%83%D1%80%D0%B8.%20%D0%94%D0%B5%D0%BA%D0%B0%D0%BB%D1%80%D0%B0%D1%86%D0%B8%D1%8F%20%D0%B8%20%D0%B8%D0%B7%D0%BF%D0%BE%D0%BB%D0%B7%D0%B2%D0%B0%D0%BD%D0%B5%20%D0%BD%D0%B0%20%D1%81%D1%82%D1%80%D1%83%D0%BA%D1%82%D1%83%D1%80%D0%B8..pdf
// Ex 1
struct comp { // a + b * i
double a;
double b;
};
comp sum(comp x, comp y) {
return { x.a + y.a, x.b + y.b };
}
void printComp(comp x) {
std::cout << x.a << " + " << x.b << " * i";
}
void ex1() {
comp first = { 3, 5 };
comp second = { 7, 25 };
printComp(sum(first, second));
}
// Ex 2
struct Student {
int fn;
int course;
int grade;
};
Student initStudent(int fn, int course, int grade) {
return { fn, course, grade };
}
void printStudent(const Student& st) {
std::cout << "FN: " << st.fn << ";COURSE: " << st.course << ";GRADE: " << st.grade << std::endl;
}
void ex2() {
Student gosho = initStudent(12345, 1, 7);
printStudent(gosho);
}
// Ex 3
struct StudentList {
Student * students;
int size;
};
struct Group {
StudentList students;
int avgGrade;
};
Group cinGroup() {
int n;
std::cin >> n;
Student* students = new Student[n];
int gradeSum = 0;
for (int i = 0; i < n; i++) {
std::cin >> students[i].fn >> students[i].course >> students[i].grade;
gradeSum += students[i].grade;
}
return { { students, n }, gradeSum / n };
}
int countScolarship(const Group& gr, int minGrade) {
int count = 0;
for (int i = 0; i < gr.students.size; i++)
if (gr.students.students[i].grade >= minGrade) count++;
return count;
}
void ex3() {
Group ourGroup = cinGroup();
int minGrade;
std::cin >> minGrade;
std::cout << countScolarship(ourGroup, minGrade) << std::endl;
delete[] ourGroup.students.students;
}
// Ex 4
void orderByGrades(const StudentList& sl) {
for (int i = 0, minInd = i; i < sl.size; i++, minInd = i) {
for (int j = i; j < sl.size; j++) {
if (sl.students[j].grade > sl.students[minInd].grade)
minInd = j;
}
Student temp = sl.students[i];
sl.students[i] = sl.students[minInd];
sl.students[minInd] = temp;
}
}
StudentList scolarshipStudents(const Group& gr, int scolarshipGrade) {
StudentList res = { new Student[gr.students.size], gr.students.size };
int ind = 0;
for (int i = 0; i < gr.students.size; i++)
if (gr.students.students[i].grade >= scolarshipGrade)
res.students[ind++] = gr.students.students[i];
res.size = ind;
return res;
}
void ex4() {
Group ourGroup = cinGroup();
int scolarshipGrade;
std::cin >> scolarshipGrade;
StudentList withScolarship = scolarshipStudents(ourGroup, scolarshipGrade);
orderByGrades(withScolarship);
for (int i = 0; i < withScolarship.size; i++)
printStudent(withScolarship.students[i]);
delete[] ourGroup.students.students;
delete[] withScolarship.students;
}
int main() {
ex4();
}
|