#include <stdio.h>
#define NUM 5 /* number of elements in theArray (can be any number of elements) */
/* function prototypes */
int array_sum(int theArray[]);
float array_avg(int theArray[]);
int array_min(int theArray[]);
int main()
{
int theArray[NUM] = {-2,11,3,5,9}; /* array to process */
int sum; /* sum of the elements */
int avg; /* average of the elements */
int min; /* minimum element in the array */
sum = array_sum(theArray);
avg = array_avg(theArray);
min = array_min(theArray);
printf("The sum is %i\n", sum
); printf("The average is %d\n", avg
); printf("The minimum is %i\n", min
);
return(0);
}
/****************************************************************/
/* Function: array_sum */
/* */
/* Purpose: Finds the sum of all of the elements in theArray. */
/* */
/* Parameters: sum - sum of the elements */
/* */
/* Returns: sum - the sum of all the elements */
/****************************************************************/
int array_sum(int theArray[])
{
int count;
int sum;
sum=0;
for (count=0; count < NUM; ++count)
{
sum = sum + theArray[count];
}
return(sum);
}
/****************************************************************/
/* Function: array_avg */
/* */
/* Purpose: Finds the avg of all of the elements in theArray. */
/* */
/* Parameters: sum - sum of the elements */
/* avg - average of the elements */
/* Returns: avg-the average of all of the elements */
/****************************************************************/
float array_avg(int theArray[])
{
int count;
int sum;
float avg;
sum=0;
for (count=0; count < NUM; ++count)
{
sum = sum + theArray[count];
avg = (sum + theArray[count])/NUM;
}
return(avg);
}
/****************************************************************/
/* Function: array_min */
/* */
/* Purpose: Finds the minimum element in theArray. */
/* */
/* Parameters: min- the minimum element */
/* */
/* Returns: The minimum element in theArray */
/****************************************************************/
int array_min(int theArray[])
{
int count;
int min;
min = theArray[0];
for(count = 1; count < NUM; ++count)
{
if(min > theArray[count])
min = theArray[count];
}
return(min);
}