Skip to content
Snippets Groups Projects
Commit bf97bb95 authored by Bruce Cowan's avatar Bruce Cowan :airplane:
Browse files

Remove mathematical functions from library, and rename to average

A bit too specialist, and a better name for the test executable
parent 721ed2f0
No related branches found
No related tags found
No related merge requests found
......@@ -252,43 +252,6 @@ array_get_capacity (const Array *array)
return array->capacity;
}
/**
* array_total_val:
* @array: An array
* @func: A function which converts the contained data into doubles
*
* Gets the total value of the array.
*
* Returns: The total value of the array.
*/
double
array_total_val (const Array *array,
ValueFunc func)
{
double total = 0;
for (int i = 0; i < array->length; i++)
total += func (array->data[i]);
return total;
}
/**
* array_average_val:
* @array: An array
* @func: A function which converts the contained data into ints
*
* Gets the average value of the array.
*
* Returns: The average value of the array.
*/
double
array_average_val (const Array *array,
ValueFunc func)
{
return array_total_val (array, func) / array->length;
}
/**
* array_sort:
* @array: An array
......
......@@ -29,12 +29,6 @@ const void * array_get (const Array *array,
size_t array_get_length (const Array *array);
size_t array_get_capacity (const Array *array);
// Aggregate functions
double array_total_val (const Array *array,
ValueFunc func);
double array_average_val (const Array *array,
ValueFunc func);
// Misc
void array_sort (Array *array,
CompareFunc func);
......@@ -6,6 +6,26 @@
#define INT_TO_PTR(x) ((void *) (size_t) (x))
#define PTR_TO_INT(x) ((int) (size_t) (x))
static double
total_val (const Array *array,
ValueFunc func)
{
double total = 0;
size_t length = array_get_length (array);
for (size_t i = 0; i < length; i++)
total += func (array_get (array, i));
return total;
}
static double
average_val (const Array *array,
ValueFunc func)
{
return total_val (array, func) / array_get_length (array);
}
static double
array_to_int (const void *data)
{
......@@ -19,8 +39,8 @@ print_array (Array *array)
printf ("Array at %p is of length %zu\n", array, length);
printf ("Array has capacity %zu\n", array_get_capacity (array));
double total = array_total_val (array, array_to_int);
double average = array_average_val (array, array_to_int);
double total = total_val (array, array_to_int);
double average = average_val (array, array_to_int);
printf ("Array total is %lf\n", total);
printf ("Array average is %lf\n", average);
......
......@@ -8,4 +8,4 @@ l_src = ['array.c', 'mem.c']
lib = shared_library('array', l_src, dependencies: m)
executable('testarray', 'testarray.c', link_with: lib)
executable('pavg', 'pavg.c', link_with: lib)
executable('average', 'average.c', link_with: lib)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment