-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.cpp
More file actions
91 lines (64 loc) · 1.93 KB
/
stats.cpp
File metadata and controls
91 lines (64 loc) · 1.93 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
//Abdul Riyan
//Orange Inc.
#include<iostream>
#include<vector>
#include<unordered_map>
#include<bits/stdc++.h>
using namespace std;
int main(){
int sum=0,mean,val,median_1=0,median_2=0,over;
double element;
cout<<"Welcome to Statistics Machine : ";
cout<<"Press the corresponding number to perform a task :\n1. Mean\n2. Median\n3. Mode\n";
cout<<"Your Input : ";
cin>>val;
vector<double> v;
cout<<"Enter the elements of the data stream & press -1 once you are done : \n";
while(true){
cin>>element;
if(element==-1){
break;
}else{
v.push_back(element);
}
}
v.pop_back();
sort(v.begin(),v.end());
int size=v.size()+1;
if(val==1){
for(int i=0;i<size;i++){
sum+=v[i];
}
mean=sum/size;
cout<<"\nThe mean of the data stream is : "<<mean;
}else if(val==2){
if(size%2==0){
median_1= (size/2)-1;
median_2 = (size/2);
}else{
median_1=(size)/2;
}
if(size%2==0){
cout<<"The median of the given data stream is : "<<v[median_1]<<" and "<<v[median_2];
}else{
cout<<"The median of the given data stream is : "<<v[median_1];
}
}else if(val==3){
unordered_map<int, int> freq;
for (int i = 0; i < size; i++) {
freq[v[i]]++;
}
int mode = -1;
int max_freq = 0;
for (auto f : freq) {
if (f.second > max_freq) {
mode = f.first;
max_freq = f.second;
}
}
cout << "The mode of the given data stream is: " << mode << endl;
}
cout<<"\nPress 0 to close the application : ";
cin>>over;
return 0;
}