-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurvey_class.cpp
More file actions
171 lines (128 loc) · 4.81 KB
/
survey_class.cpp
File metadata and controls
171 lines (128 loc) · 4.81 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <exception>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
// The Survey class provides functionality to add values, calculate statistics such as
// average, quartiles, and standard deviation, and connect surveys to combine data sets.
// It handles cases where the survey is empty and ensures proper error handling.
class Survey {
public:
// Default constructor
Survey(): value() {};
Survey(unsigned int _number_): value(_number_){};
// Basic desctructor
~Survey() = default;
// Copy constructor
Survey(const Survey& u) = default;
// Add new value (double) to Survey
void add(double _value_){
value.push_back(_value_);
}
// Return size of Survey
int numSurvey() const{
return value.size();
}
// Return average of elements in Survey
double avg() const;
// Return proper quartile: quartile(3);
double quartile(int _number_) const;
// Return standard deviation
double std_dev() const;
// Connects Survey p to Survey
Survey& connect_surveys(const Survey& p){
value.insert(value.end(), p.value.begin(), p.value.end());
return *this;
}
private:
// Vector for value that represents the set of measurements
vector<double> value;
};
double Survey::avg() const{
if(value.empty()){
throw logic_error("Survey is empty, can't find average value");
}
//sum := sum of all elements in Survey
double sum = 0.0;
for(double i: value){
sum += i;
}
return sum / value.size();
}
double Survey::quartile(int _number_) const{
if(value.empty()){
throw logic_error("Survey cannot be an empty set in quartile()");
}
// Make a copy of the vector before sorting
vector<double> sortedValues = value;
sort(sortedValues.begin(), sortedValues.end());
// n := size of the vector
size_t n = sortedValues.size();
switch(_number_){
case 2:
return n % 2 == 0 ? (sortedValues[n - 1] + sortedValues[n]) / 2 : sortedValues[n - 1];
case 1:
{
// mid := possition of middle element - last element of left half of array
int mid = n / 2 - 1;
return mid % 2 == 0 ? (sortedValues[mid - 1] + sortedValues[mid]) / 2 : sortedValues[mid - 1];
}
case 3:
{
int mid = n % 2 == 0 ? n / 2 : n / 2 + 1;
return (n - mid) % 2 == 0 ? (sortedValues[(n + mid) / 2 - 1] + sortedValues[(n + mid) / 2]) / 2 : sortedValues[(n + mid) / 2];
}
case 4:
return sortedValues[n - 1];
default:
throw logic_error("Value for quartile() must be in the {1, 2, 3, 4} set");
}
}
double Survey::std_dev() const{
if(value.empty()){
throw logic_error("Survey cannot be an empty set in set_dev()");
}
// x:= avg value
double x = avg();
double sum = 0;
for(double i : value){
sum += (i - x) * (i - x);
}
sum /= (value.size() - 1);
return sqrt(sum);
}
int main(){
// Create instances of Survey
Survey survey1;
Survey survey2;
Survey survey3;
// Add values to surveys
survey1.add(10.5);
survey1.add(15.2);
survey1.add(20.8);
survey2.add(25.1);
survey2.add(30.7);
survey2.add(35.4);
// Connect surveys
survey1.connect_surveys(survey2);
// Display the size of the combined survey
cout << "Combined survey size: " << survey1.numSurvey() << "\n";
// Calculate and display the average of values in the combined survey
cout << "Average value in the combined survey: " << survey1.avg() << "\n";
// Calculate and display the first quartile of values in the combined survey
cout << "First quartile in the combined survey: " << survey1.quartile(1) << "\n";
// Calculate and display the third quartile of values in the combined survey
cout << "Third quartile in the combined survey: " << survey1.quartile(3) << "\n";
// Calculate and display the standard deviation of values in the combined survey
cout << "Standard deviation in the combined survey: " << survey1.std_dev() << "\n";
cout<<"\n";
// Calculate and display the average of values in the second survey
cout << "Average value in the second survey: " << survey2.avg() << "\n";
// Calculate and display the first quartile of values in the second survey
cout << "First quartile in the second survey: " << survey2.quartile(1) << "\n";
// Calculate and display the third quartile of values in the second survey
cout << "Third quartile in the second survey: " << survey2.quartile(3) << "\n";
// Calculate and display the standard deviation of values in the second survey
cout << "Standard deviation in the second survey: " << survey2.std_dev() << "\n";
return 0;
}