/* Hightemp.c */
/* This program averages the high temperatures over a user-defined */
/* number of days. */

#include <stdio.h>
#define array_size 32  /* constant to define size of array */

int main()
{
  int daily_temp[array_size]; /* array of daily high temperatures */
  int num_values;             /* number of days in a row to enter values */
  int index;                  /* index for loop counter and array access */
  float average_high;         /* calculated average high temperature */
  int total = 0;              /* used to total temps before averaging */

  do  /* loop to ask for number of days until valid input is received */
  {
    printf("Enter the number of days for which you have data: ");
    scanf("%d", &num_values);
    if ((num_values < 1) || (num_values > array_size - 1))
     {
      printf("The number of days must be in the range 1 to %d.\n",
         array_size - 1);
     }
  } while ((num_values < 1) || (num_values > array_size - 1));

  /* The following loop gets the high temperatures from the user for as */
  /* many days as the user specified in num_values. The subscript 0 is */
  /* not used so that the subscript will correspond with the day number. */
  for(index = 1; index <= num_values; index++)
   {
    printf("Enter the high temperature for day %d: ", index);
    scanf("%d", &daily_temp[index]);  /* input value into array */
   }

  /* Print the values in the array to the screen. */
  printf("The array contains high temperatures for %d days.\n", num_values);
  printf("The values are as follows.\n");
  for(index = 1; index <= num_values; index++)
   {
    printf("Day %d: %d\n", index, daily_temp[index]);
    total = total + daily_temp[index]; /* update total for averaging */
   }

  /* Calculate average by typecasting total and num_values to floats */
  /* before dividing and assigning the result to average_high. */
  average_high = (float) total / (float) num_values;

  /* Print the results to the screen. */
  printf("The average high temperature during the %d", num_values);
  printf("-day period was %.2f degrees.\n", average_high);

  return 0;
}
