/* --------------------------------------------------------------------------
Name:          functions.c
Function:      Code snippet to illustrate function calls and passing array pointers
Versions:
   1.0   2/5/2007   L Simone.  Created.  File compiled for debugging mode.
----------------------------------------------------------------------------- */

#define NUM_TEMPERATURE_SAMPLES 10

/* Global Variables  */
char temperature[NUM_TEMPERATURE_SAMPLES] = {1,2,3,4,5,6,7,8,9,10};
char ave_temperature = 0;     /* Average temperature           */
char *temperature_ptr;        /* Pointer to temperature array  */

/* Function Redeclarations */
extern char mean(char *array, char num_samples);



void main( void )
{
   /* Get a pointer to the start of the temperature array   */
   temperature_ptr = &temperature[0];

   /* Compute the average temperature over the entire array */
   ave_temperature = mean(temperature_ptr, NUM_TEMPERATURE_SAMPLES);  
}

/* --------------------------------------------------------------------------
Name:       mean
Function:   Compute the mean of an array
Arguments:  array: pointer to the incoming array of data
            num_samples: number of samples in the incoming array
Returns:    returns the array average 
Notes:      if num_samples gets larger than 256, need to change function to accept 
            an integer, and also change the loop counter, i.
----------------------------------------------------------------------------- */
char mean(char *array, char num_samples)
{
  char i;            /* loop counter         */
  int sum = 0;       /* temperary variable used for summing and averaging */
  
  /* Compute the sum of the samples */
  
  for(i=0; i<num_samples; i++)
  {
    sum = sum + array[i];     /* Could also be written sum += array[i]; */
  }
  
  /* Compute the average */
  sum = sum/num_samples;      /* Could also be written sum /= num_samples */

  return sum;
}

