pico-scale
util.h File Reference
#include <assert.h>
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Functions

void util_average (const int32_t *const arr, const size_t len, double *const avg)
 Calculates the average value from an array of signed 32-bit integers. More...
 
void util_median (int32_t *const arr, const size_t len, double *const med)
 Calculates the median value from an array of signed 32-bit integers. More...
 
int util__median_compare_func (const void *a, const void *b)
 

Function Documentation

◆ util__median_compare_func()

int util__median_compare_func ( const void *  a,
const void *  b 
)

Definition at line 78 of file util.c.

80  {
81 
82  assert(a != NULL);
83  assert(b != NULL);
84 
85  const int32_t* restrict const pA = (const int32_t* const)a;
86  const int32_t* restrict const pB = (const int32_t* const)b;
87 
88  //https://stackoverflow.com/a/10996555/570787
89  return (*pA < *pB) ? -1 : (*pA > *pB);
90 
91 }

Referenced by util_median().

◆ util_average()

void util_average ( const int32_t *const  arr,
const size_t  len,
double *const  avg 
)

Calculates the average value from an array of signed 32-bit integers.

Parameters
arrarray of values
lennumber of values in the array
avg

Definition at line 27 of file util.c.

30  {
31 
32  assert(arr != NULL);
33  assert(len > 0);
34  assert(avg != NULL);
35 
36  long long int total = 0;
37 
38  for(size_t i = 0; i < len; ++i) {
39  total += arr[i];
40  }
41 
42  *avg = (double)total / len;
43 
44 }

Referenced by scale_read().

◆ util_median()

void util_median ( int32_t *const  arr,
const size_t  len,
double *const  med 
)

Calculates the median value from an array of signed 32-bit integers.

Parameters
arrarray of values
lennumber of values in the array
med

If the number of elements is even, the median is the average of the middle two elements. Otherwise it is the middle element.

Definition at line 46 of file util.c.

49  {
50 
51  assert(arr != NULL);
52  assert(len > 0);
53  assert(med != NULL);
54 
55  qsort(
56  arr,
57  len,
58  sizeof(int32_t),
60 
66  if (len % 2 == 0) {
67  *med = (arr[(len / 2) - 1] + arr[len / 2]) / 2.0;
68  }
69  else if (len > 1) {
70  *med = (double)arr[(len / 2) + 1];
71  }
72  else {
73  *med = (double)arr[0];
74  }
75 
76 }
int util__median_compare_func(const void *a, const void *b)
Definition: util.c:78

References util__median_compare_func().

Referenced by scale_read().