-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuchar_array.c
More file actions
95 lines (86 loc) · 2.47 KB
/
uchar_array.c
File metadata and controls
95 lines (86 loc) · 2.47 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
/**
* @file uchar_array.c
* @brief Definition of and functions for unsigned character array.
*/
/**
* @brief A struct to hold a unsigned char array.
* Also holds the size, and the standard deviation.
*/
typedef struct {
/** The unsigned char array. */
unsigned char *array;
/** The standard deviation of the values in the unsigned char array. */
double standard_dev;
/** The number of elements in the unsigned char array. */
int size;
} uchar_array_t;
/**
* @brief Finds the maximum value inside a given unsigned char array.
* Note that if the array is empty then the maximum value is zero.
* @param data The unsigned char array.
* @returns The maximum value.
*/
unsigned char max(uchar_array_t *data) {
unsigned char result = 0;
for (int i = 0; i < data->size; i++) {
if (data->array[i] > result) {
result = data->array[i];
}
}
return result;
}
/**
* @brief Finds the average value of a given unsigned char array.
* @param data The unsigned char array.
* @returns The average value.
*/
double avg(uchar_array_t *data) {
int total = 0;
for (int i = 0; i < data->size; i++) {
total += data->array[i];
}
return (total / data->size);
}
/**
* @brief Sets the standard deviation of a given uchar_array_t struct.
* @param data The uchar_array_t struct.
*/
void standard_dev(uchar_array_t *data) {
double average = avg(data);
double total = 0;
printf("%i\n", data->size);
for (int i = 0; i < data->size; i++) {
total += pow(data->array[i] - average, 2);
}
total /= data->size;
data->standard_dev = sqrt(total);
}
/**
* @brief Finds the lower quartile of a given unsigned char array.
* @param data The unsigned char array.
* @param dev The deviation.
* @returns The lower quartile.
*/
unsigned char lower(uchar_array_t *data, double dev) {
return avg(data) - (dev * data->standard_dev);
}
/**
* @brief Finds the upper quartile of a given unsigned char array.
* @param data The unsigned char array.
* @param dev The deviation.
* @returns The upper quartile.
*/
unsigned char upper(uchar_array_t *data, double dev) {
return avg(data) + (dev * data->standard_dev);
}
/**
* @brief Initialises a uchar_array_t struct.
* @param size The number of elements in the array.
* @returns The pointer to the initialised struct.
*/
uchar_array_t *init_arr(int size) {
uchar_array_t *arr = (uchar_array_t *) malloc(sizeof(uchar_array_t));
arr->size = size;
arr->array = (unsigned char *) malloc(sizeof(unsigned char) * size);
return arr;
}