-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.cpp
61 lines (51 loc) · 1.05 KB
/
vector.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
61
#include <iostream>
#include <vector>
#include <cmath>
void initialize(std::vector<double> & a);
void print(const std::vector<double> & a);
double average(const std::vector<double> & a);
double dstandard(const std::vector<double> & a, double p);
int main(void)
{
const int N=10;
std::vector<double> x(N);
initialize(x);
print(x);
std::cout<< average(x)<< std::endl;
double p = average(x);
std::cout <<dstandard(x,p)<<std::endl;
return 0;
}
void initialize(std::vector<double> & a)
{
for (int ii=0; ii < a.size(); ++ii){
a[ii]=ii*2;
}
}
void print(const std::vector<double> & a)
{
for(int ii=0; ii <a.size(); ++ii){
std::cout<<a[ii]<<std::endl;
}
}
double average(const std::vector<double> & a)
{
double f;
double sum=0.0;
for(int ii=0; ii< a.size(); ++ii){
sum+= a[ii];
}
f=sum/a.size();
return f;
}
double dstandard(const std::vector<double> & a, double p)
{
double sum=0;
double d=1;
double p1=p;
for(int ii=0; ii< a.size(); ++ii){
sum+=std::pow(a[ii]-p1,2);
}
d=sum/(a.size()-1);
return d;
}