/* Program to print and process precitipation in six cities,
   based on totals recorded during the eight days December 18-25, 2014.
*/

#include <stdio.h>
#define NUM_CITIES 6
#define NUM_DAYS 8

/* function returns the total rainfall for the city with the given number */
double
computeCityPrecip (double rain [NUM_DAYS][NUM_CITIES], int city);

int
main (void)
{

  char * cities [NUM_CITIES] = {"Chicago", "Denver", "Des Moines", 
                                "Phoenix", "San Jose", "Seattle"};
  char * states [NUM_CITIES] = {"Illinois", "Colorado", "Iowa",
                                "Arizona", "California", "Washington"};
  double precip [NUM_DAYS][NUM_CITIES] = { 
            /* Dec. 18 */  { 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.51 }, 
            /* Dec. 19 */  { 0.00 , 0.00 , 0.00 , 0.00 , 0.19 , 0.12 }, 
            /* Dec. 20 */  { 0.00 , 0.00 , 0.00 , 0.00 , 0.06 , 0.77 },
            /* Dec. 21 */  { 0.00 , 0.00 , 0.00 , 0.00 , 0.00 , 0.00 },  
            /* Dec. 22 */  { 0.28 , 0.14 , 0.34 , 0.00 , 0.00 , 0.00 },  
            /* Dec. 23 */  { 0.13 , 0.00 , 0.34 , 0.00 , 0.02 , 0.81 },  
            /* Dec. 24 */  { 0.12 , 0.00 , 0.09 , 0.00 , 0.04 , 0.21 },   
            /* Dec. 25 */  { 0.00 , 0.21 , 0.00 , 0.00 , 0.00 , 0.00 }   
                         };
/* source: 
    http://www.accuweather.com/en/us/chicago-il/60608/weather-forecast/348308 
    and similar pages from accuweather, accessed December 27, 2014.
*/

  int day, city; /* array index / loop variables */
  /* print titles */
  printf ("Precipitation at six cities over eight days in December 2014\n");
  printf ("         ");
  for (city = 0; city < NUM_CITIES; city++)
    printf ("%-11s", cities[city]);
  printf ("\n");
  printf ("  Date   ");
  for (city = 0; city < NUM_CITIES; city++)
    printf ("%-11s", states[city]);
  printf ("\n\n");

  /* print precipitation table */
  for (day = 0; day < NUM_DAYS; day++)
  {
    printf ("Dec. %2d", day+18);
    for (city = 0; city < NUM_CITIES; city++)
    {
      printf ("   %5.2lf   ", precip[day][city]);
    }
    printf ("\n");
  }
  printf ("\n");

  /* print total precipitation for each city */
  printf ("Totals ");
  for (city = 0; city < NUM_CITIES; city++)
  {
    printf ("   %5.2lf   ", computeCityPrecip (precip, city));
  }
  printf ("\n\n");
 
  return 0;
} // main


/* function returns the total rainfall for the city with the given number */
double
computeCityPrecip (double rain [NUM_DAYS][NUM_CITIES], int city)
{
  double sum = 0.0;
  for (int day = 0; day < NUM_DAYS; day++)
    sum += rain [day][city];
  return sum;
} // computecityPrecip

