For this assignment you have been given a program which asks for an array from the user and calculates the maximum value in the array. Compile and run this program before you proceed.
You are to add 3 more functions to this program which calculate the minimum, sum, and average of the values in the array.
Here are the prototypes and documentation for the functions you need to write. Copy and paste the prototypes and documentation to your code one-by-one as you implement the functions:
/**
* Find the minimum value in an array of integers. The array must contain at
* least one element.
*
* Parameters:
* array - the array to search
* size - the number of elements in array
*
* Return value:
* The minimum value in the array
*/
int min_value(const int array[], size_t size);
/**
* Find the sum of an array of integers. The array must contain at least
* one element.
*
* Parameters:
* array - the array on which to calculate the sum
* size - the number of elements in array
*
* Return value:
* The sum of the values in the array
*/
int sum(const int array[], size_t size);
/**
* Find the average 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 average of the values in the array
*/
double average(const int array[], size_t size);
min_value()
The min_value()
function will be very similar to the max_value()
function, you will just need to check for each value being smaller than the min rather than larger than the max.
sum()
To calculate the sum you will need to set a sum variable to 0 and then loop over the array elements, adding the value of each element to the sum.
average()
Calculating the average simply involves calculating the sum and then dividing the sum by the size. Write this function after you write sum()
so that you can use the sum()
function within this function.
Remember that an int
divided by an int
is going to give you an int
and we want the average as a double
. So you will need to cast either the sum or the size to a double
in your division. To cast a variable named var
to a double
you would write (double)var
.
For full credit on this assignment your average()
function must not include a loop (use the sum()
function instead).
main()
The main()
function contains the following lines:
printf("Max: %i\n", max_value(array, array_size));
//printf("Min: %i\n", min_value(array, array_size));
//printf("Sum: %i\n", sum(array, array_size));
//printf("Average: %lf\n", average(array, array_size));
Uncomment each of the commented out lines as you complete each function.