/* Program to demonstrate the use of a debugger when program does not crash */
#include <stdio.h>

/* Compute the average of a list of numbers of a given length
 *
 * Preconditions:
 *  values refers to an array of the specified length
 *  average points to a valid address
 * Postconditions:
 *  *average contains the arithmetic mean of the numbers in values
 */
void
compute_average (float values[], int length, float * average);

/* Halve each number in an array
 *
 * Preconditions:
 *  values refers to an array of the specified length
 * Postconditions:
 *  After running, values[i] (for 0 <= i < length) is half what it was before
 */
void
halve (float values[], int length);

/* Demonstrate the procedures working 
 * 
 * Postconditions:
 *  Prints the expected average of the numerals 1..6
 *  Prints the calculated average of the numerals 1..6 from an array
 */
int
main (void)
{
  float sample[] = {1,2,3,4,5,6};
  short length = 6;

  float mean;
  float expected = (length+1)/2.0;

  compute_average (sample,length,&mean);
  halve (sample,length);

  printf ("expected = %1.2f\n",expected);
  printf ("mean = %1.2f\n",mean);
  printf ("length = %d\n",length);

  return 0;
} // main


void
compute_average (float values[], int length, float * average)
{
  int i;
  for (i=0 ; i <= length ; i++)
    {
      *average += values[i]/length;
    }
} // compute_average

void
halve (float values[], int length)
{
  int i;
  for (i=0 ; i <= length ; i++)
    {
      values[i] /= 2;
    }
} // halve
