Stats Module Exercise

For this assignment you will add one additional function to a C module.

I will reiterate here that you should download the example files from the last video: triangle_module.zip. The files in this example contain useful comments, particularly test_triangle.c and Makefile.

Code Structure

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.

Implementing range()

The range() function must have the following prototype, 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.

Testing range()

In main.c, simply uncomment the line that prints out the range so that you can test your function.

Submission

Once you are confident that your function works, add main.c, stats.h, and stats.c with git, 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.