For this assignment you will add one additional function to a C module.
It is important to note that you should download the example files from the Modules and Makefiles lecture: triangle_module.zip. The files in this example contain useful comments, particularly test_triangle.c and 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 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.
Once you are confident that your function works, add main.c
, stats.h
, and stats.c
with git and then commit and push your code to git-keeper. git-keeper will run my tests against your module.
For full credit all the tests must pass, your range()
function must use a single loop and not call any other functions, and your range()
function must not print out any values within the function itself.
Your grade for this assignment will be out out of 3 points: