For this assignment you will add one additional function to a C module.
It is important to note that you should review the example files from the Modules and Makefiles example code on GitHub. The files in this example contain useful comments, particularly test_triangle.c
and the Makefile
.
You are given several files which implement a working program: stats.h
and stats.c
which contain the interface and implementation for a module; main.c
which contains code to ask the user for values to place in an array and then prints the stats; and a Makefile
.
If you cd
to the assignment directory and then run make
it will build the project, and then you can run ./stats
to run the program. Run the program as it is before editing any of the files so you can see how this works with a properly functioning program.
The example Makefile from the video contains comments which explain what make is doing and the basic structure of a Makefile.
range()
The range()
function must have the following prototype and comment, which you will need to place in stats.h
:
/**
* Calculate the difference between the maximum value and the minimum value of
* an array of integers. The array must contain at least one element.
*
* Parameters:
* array - the array on which to calculate the average
* size - the number of elements in array
*
* Return value:
* The range of the values of the array
*/
int range(const int array[], size_t size);
Write the function implementation in stats.c
. The implementation of this function could be written very simply:
return max_value(array, size) - min_value(array, size)
However, this is not efficient for large arrays. Using this method you will end up looping over the array twice, since max_value()
and min_value()
both loop over the array.
Instead, write a single loop which finds both the minimum and maximum values in one pass.
range()
In main.c
, simply uncomment the line that prints out the range so that you can test your function. You can also run the gh-test-stats
target from the Makefile.