/*
  Several operations on an array (largest/smallest, average, etc)
*/

#include <stdio.h>

main()
{
  /*  Declaration Statements  */
  char month[12][4];
  short units[12];
  double sales[12];
  short uq[4];
  double sq[4];
  short i, cars, mins, maxs, j, k, iave;
  double totals, big, min, ave;

  printf("C54.C -> Operations an array \n");

  /*  Assignment Statements  */

  for (i = 1; i <= 12; i++)
  {                                      /* Read value from */
    printf("Month (Jan,Feb,...) : ");    /* keyboard entry. */
    scanf("%s",month[i-1]);
    printf("Units Sold          : ");
    scanf("%hd", &units[i - 1]);
    printf("Sales (in million $): ");
    scanf("%lg", &sales[i - 1]);
  }

  /*  Initializing variables  */
  cars = units[0];
  totals = sales[0];
  big = totals;
  min = totals;
  mins = 1;
  maxs = 1;

  for (i = 2; i <= 12; i++)
  {
    cars += units[i - 1];
    totals += sales[i - 1];
    if (sales[i - 1] > big)     /* It selects the biggest sales  */
    {                           /* and keep track of the index   */
      big = sales[i - 1];       /* to know when it occurs.       */
      maxs = i;
    }  /*  End of if statement  */
    else
    {
       if (sales[i - 1] < min)  /* It selects the smallest sales */
       {                        /* and keep track of the index   */
       min = sales[i - 1];      /* to know when it occurs.       */
       mins = i;
       }
    }  /*  End of else statement  */
  }  /*  End of for{} loop  */

  i = 0;
  for (j = 1; j <= 4; j++)
  {
    uq[j - 1] = 0;
    sq[j - 1] = 0.0;
    for (k = 1; k <= 3; k++)
    {
      i++;
      uq[j - 1] += units[i - 1];
      sq[j - 1] += sales[i - 1];
    }  /*  End of inner for{} loop  */
  }  /*  End of outer for{} loop  */

  /*  Output original data and results  */

  printf("\n");
  printf("Month unit  sales m$\n");
  for (k = 1; k <= 12; k++)
    printf("%3c%s%6d%6.1f\n",' ', month[k-1], units[k-1], sales[k-1]);
  printf("\nTotals%5d%6.1f\n", cars, totals);

  iave = cars / 12;      /*  Compute the units sold average */
  ave = totals / 12.0;   /*  and sales average by month.    */

  printf("\Nave.%7d%6.1f\n\n", iave, ave);
  printf("Best/worst-%5.1f%5.1f\n\n", big, min);
  printf("Occurred in%2c%s%2c%s\n\n",
          ' ', month[maxs - 1], ' ', month[mins - 1]);
  printf("Cars sold by quarter%5d%5d%5d%5d\n", uq[0], uq[1], uq[2],
          uq[3]);
  printf("Sales by quarter    %5.1f%5.1f%5.1f%5.1f\n",
          sq[0], sq[1], sq[2], sq[3]);

  return(0);
}
/*  End of Program C54  */