-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverageNew.c
More file actions
58 lines (44 loc) · 1.69 KB
/
averageNew.c
File metadata and controls
58 lines (44 loc) · 1.69 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
/*#include <stdio.h>
#define SIZE 30
#define MA_SIZE 5
void calculate_cumulative(double exchange_rate[], double cumulative_average[], int size) {
double total = 0;
for (int i = 0; i < size; i++) {
total += exchange_rate[i];
cumulative_average[i] = total / (i + 1);
}
}
void calculate_moving(double exchange_rate[], double moving_average[], int size, int ma_size) {
for (int i = 0; i < size; i++) {
moving_average[i] = 0;
}
for (int i = ma_size - 1; i < size; i++) {
double sub_total = 0;
for (int j = i; j > i - ma_size; j--) {
sub_total += exchange_rate[j];
}
moving_average[i] = sub_total / ma_size;
}
}
void print_averages(double exchange_rate[], double cumulative_average[], double moving_average[], int size) {
printf("Exchange Rate - Cumulative Average - Moving Average\n");
for (int i = 0; i < size; i++) {
printf("|%.3lf |%.3lf |%.3lf |\n", exchange_rate[i], cumulative_average[i],
moving_average[i]);
}
}
int main() {
double exchange_rate[SIZE] = {3.74, 3.75, 3.80, 3.78, 3.78, 3.78, 3.79, 3.78, 3.78, 3.80,
3.79, 3.80, 3.80, 3.80, 3.80, 3.80, 3.79, 3.79, 3.82, 3.81,
3.82, 3.81, 3.84, 3.86, 3.87, 3.89, 3.91, 3.91, 3.91, 3.93
};
double cumulative_average[SIZE];
calculate_cumulative(exchange_rate, cumulative_average, SIZE);
double moving_average[SIZE];
calculate_moving(exchange_rate, moving_average, SIZE, MA_SIZE);
print_averages(exchange_rate, cumulative_average, moving_average, SIZE);
return 0;
}*/
//
// Created by User on 21.04.2022.
//