-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode1.cpp
More file actions
74 lines (56 loc) · 1.59 KB
/
Copy pathcode1.cpp
File metadata and controls
74 lines (56 loc) · 1.59 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
//Name-Kamalpreet
//Roll No.-2010990354
//Set-01
#include <bits/stdc++.h>
using namespace std;
long mean(vector<long> grades){
return accumulate(begin(grades), end(grades), (long)0)/grades.size();
}
long median(vector<long> grades, int size){
sort(begin(grades), end(grades));
if(size % 2 != 0){
return grades[size/2];
}
int a = size/2;
int b = a + 1;
return (grades[a] + grades[b])/2;
}
pair<vector<string>, int> mode(vector<string> names, vector<long> grades){
unordered_map<int, int> freq;
for(int i=0; i<grades.size(); i++)
freq[grades[i]]++;
int key, val = 0;
for(auto& p: freq){
if(p.second > val)
key = p.first;
}
pair<vector<string>, int> ans;
vector<string> studentsWithMode;
for(int i=0; i<grades.size(); i++){
if(grades[i] == freq[key])
studentsWithMode.push_back(names[i]);
}
return ans;
}
int main()
{
vector<string> names;
vector<long> grades;
int size;
cin>>size;
string name;
int grade;
for(int i=0; i<size; i++){
cin>>name>>grade;
names.push_back(name);
grades.push_back(grade);
}
cout<<"mean: "<<mean(grades)<<endl;
cout<<"median: "<<median(grades, size)<<endl;
pair<vector<string>, int> p = mode(names, grades);
cout<<"mode: "<<p.second<<endl<<"students with the mode ";
for(int i=0; p.first.size(); i++){
cout<<p.first[i]<<" ";
}
return 0;
}