Exercise 10: Structures

For this assignment you will re-implement the functionality of the stats module from a previous assignment with a single function that returns a structure. The Function

The function prototype is defined in stats.h like so:

/**
 * Calculate the min, max, sum, range, and average of an array.
 *
 * Parameters:
 *   array - the array on which to calculate the stats, must not be empty
 *   size - the number of elements in the array
 *
 * Return:
 *   A stat_values struct containing stats from the array
 */
struct stat_values calculate_stats(const int array[], size_t size);

The structure stat_values is also defined for you in stats.h:

struct stat_values {
    int min;
    int max;
    int sum;
    int range;
    double average;
};

Since calculate_stats() returns an instance of the stat_values structure, all of these values will be returned at once within the instance. You will need to declare an instance of struct stat_values, assign values to its members, and then return the instance. See the make_rectangle() function from the video code for an example where we wrote a function that returns an instance of a structure.

You are to write the body of the stats() function in stats.c. This is the only code that you need to edit. In the stats() function you will need to declare a stat_values structure instance, assign the appropriate values to its members, and then return it.

When we calculated these values individually, each of the functions used to calculate one of the values used a loop. Here we can gather all of the information we need to calculate the stats with a single loop. For full credit you must only use a single loop in your calculate_stats() function.

main.c

main.c contains code which asks you to enter an array, calls calculate_stats(), and prints the stats.

Submission

Push your submission to GitHub. In addition to the above requirements, you will be graded on proper style.

Grading

Your grade for this assignment will be out out of 10 points: