1
0

rain.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* rain.c -- finds yearly totals, yearly average, and monthly
  2. average for several years of rainfall data */
  3. #include <stdio.h>
  4. #define MONTHS 12 // number of months in a year
  5. #define YEARS 5 // number of years of data
  6. int main(void)
  7. {
  8. // initializing rainfall data for 2010 - 2014
  9. const float rain[YEARS][MONTHS] =
  10. {
  11. {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
  12. {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
  13. {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
  14. {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
  15. {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
  16. };
  17. int year, month;
  18. float subtot, total;
  19. printf(" YEAR RAINFALL (inches)\n");
  20. for (year = 0, total = 0; year < YEARS; year++)
  21. { // for each year, sum rainfall for each month
  22. for (month = 0, subtot = 0; month < MONTHS; month++)
  23. subtot += rain[year][month];
  24. printf("%5d %15.1f\n", 2010 + year, subtot);
  25. total += subtot; // total for all years
  26. }
  27. printf("\nThe yearly average is %.1f inches.\n\n",
  28. total/YEARS);
  29. printf("MONTHLY AVERAGES:\n\n");
  30. printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
  31. printf(" Nov Dec\n");
  32. for (month = 0; month < MONTHS; month++)
  33. { // for each month, sum rainfall over years
  34. for (year = 0, subtot =0; year < YEARS; year++)
  35. subtot += rain[year][month];
  36. printf("%4.1f ", subtot/YEARS);
  37. }
  38. printf("\n");
  39. return 0;
  40. }