/*
  Functions to find the average of parts of an array.
*/

#include <stdio.h>

typedef char char_array[12][4];  /* Variable Type Declaration */
typedef short int_array[12];
typedef double real_array[12];

char_array month;      /*  Declaration Statements  */
int_array units;
real_array sales;
short k, start, meanu, j;
double save;

void average(n, start, units, sales, iave, ave)
short n, start;
short *units;                  /*  Procedure Declaration  */
double *sales;
short *iave;
double *ave;
{
  short cars, k, j;
  double dollars;

  cars = 0;
  dollars = 0.0;
  j = start + n - 1;
  for (k = start - 1; k < j; k++)
  {
    cars += units[k];      /* To compute the sum */
    dollars += sales[k];
  }  /*  End of for{} loop  */

  *iave = cars / n;     /*  To compute the average  */
  *ave = dollars / n;
}   /*  End of average procedure  */


main()
{
  /*  Declaration Statement  */
  char *TEMP;

  clrscr();
  printf("C67.C -> Average Program 2 (again using a function)\n\n");

  /*  Assignment Statements  */

  for (k = 1; k <= 12; k++)
  {
    printf("Month (Jan,Feb,...) : ");
    scanf("%s",month[k-1]);           /* Read from keyboard entry */
    printf("Units Sold          : ");
    scanf("%hd", &units[k - 1]);
    printf("Sales (in million $): ");
    scanf("%lg", &sales[k - 1]);
  }

  for (k=1;k<=12; k++)
    printf(" %s%7d%6.1f\n", month[k - 1], units[k - 1], sales[k - 1]);

  /*
       Data has been read, stored and printed.
       Now use procedure once for each quarter.
  */

  for (j = 1; j <= 4; j++)
  {
  start = (j - 1) * 3 + 1;
  average(3,start,units,sales,&meanu,&save);  /* Call of procedure */
  printf("\nmean%7d%6.1f  quarter #%d\n", meanu, save, j);
  }  /*  End of for{} loop  */

  return(0);
}
/* End of main Program C67 */