-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject03_price_series_stats.cpp
More file actions
61 lines (49 loc) · 1.42 KB
/
Copy pathproject03_price_series_stats.cpp
File metadata and controls
61 lines (49 loc) · 1.42 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
#include <iostream>
// print: the number of ticks, the sum, the average, the min, and the max.
double getTotal(double arr[], size_t n);
double getMin(double arr[], size_t n);
double getMax(double arr[], size_t n);
double getAvg(double sum, size_t n);
int main(){
double prices[] = {101.2, 100.8, 101.5, 104.23, 99.89, 101.12};
size_t n = sizeof(prices)/sizeof(prices[0]);
double sum = getTotal(prices, n);
double average = getAvg(sum, n);
double min = getMin(prices, n);
double max = getMax(prices, n);
std::cout << "*****Price array Statistics*****" << "\n";
std::cout << "Ticks: " << n << "\n";
std::cout << "Sum: " << sum << "\n";
std::cout << "Average price: " << average << "\n";
std::cout << "Max price: " << max << "\n";
std::cout << "Min price: " << min << "\n";
return 0;
}
double getTotal(double arr[], size_t size){
double total = 0;
for(int i = 0; i < size; i++){
total += arr[i];
}
return total;
}
double getAvg(double sum, size_t n){
return sum/n;
}
double getMax(double arr[], size_t n){
double res = arr[0];
for (int i = 1; i < n; i++){
if (arr[i] >= res){
res = arr[i];
}
}
return res;
}
double getMin(double arr[], size_t n){
double res = arr[0];
for (int i = 1; i < n; i++){
if (arr[i] <= res){
res = arr[i];
}
}
return res;
}