fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 7 P.444 #2
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compute Rainfall Statistics
  6.  * ____________________________________________________________________________
  7.  * This program will calculate and display the total rainfall for the year,
  8.  * the average monthly rainfall, and the months with the highest and lowest
  9.  * amounts.
  10.  *
  11.  * Formula Used:
  12.  * average = totalRainfall / MONTHS
  13.  * ____________________________________________________________________________
  14.  * Input
  15.  * MONTHS :Number of months in a year
  16.  * monthNames :Array that stores the names of the months
  17.  * rainfall :Array that stores the total rainfall for each month
  18.  * Output
  19.  * totalRainfall :Sum of rainfall for all 12 months
  20.  * highest :Highest monthly rainfall amount entered
  21.  * lowest :Lowest monthly rainfall amount entered
  22.  * highestMonth :Index of the month with the highest rainfall
  23.  * lowestMonth :Index of the month with the lowest rainfall
  24.  * average :Average monthly rainfall
  25.  *****************************************************************************/
  26. #include <iostream>
  27. #include <iomanip>
  28. using namespace std;
  29. int main() {
  30. //Data Dictionary
  31. const int MONTHS = 12; //INPUT - Number of months in a year
  32. string monthNames[MONTHS] = {
  33. "January", "February", "March", "April", "May", "June",
  34. "July", "August", "September", "October", "November", "December"
  35. };
  36. //INPUT - stores the name of the months
  37. float rainfall[MONTHS]; //INPUT - Array that stores the total rainfall for each month
  38. float totalRainfall = 0.0; //OUTPUT - Sum of rainfall for all 12 months
  39. float highest = 0.0; //OUTPUT - Highest monthly rainfall amount entered
  40. float lowest = 0.0; //OUTPUT - Lowest monthly rainfall amount entered
  41. int highestMonth = 0; //OUTPUT - Index of the month with the highest rainfall
  42. int lowestMonth = 0; //OUTPUT - Index of the month with the lowest rainfall
  43. float average; //OUTPUT - Average monthly rainfall
  44.  
  45. //User Input
  46. cout << "Enter the total rainfall (in inches) for each month: " << endl;
  47. for(int i = 0; i < MONTHS; i++){
  48. do{
  49. cout << monthNames[i] << ": " << endl;
  50. cin >> rainfall [i];
  51. if(rainfall[i] < 0){
  52. cout << "Error: Rainfall cannot be negative. Please re-enter." << endl;
  53. }
  54. }while(rainfall[i] < 0);
  55. totalRainfall += rainfall[i];
  56. }
  57.  
  58. //Calculations
  59. highest = lowest = rainfall[0];
  60. for(int i = 1; i < MONTHS; i++){
  61. if(rainfall[i] > highest){
  62. highest = rainfall[i];
  63. highestMonth = i;
  64. }
  65. if(rainfall[i] < lowest){
  66. lowest = rainfall[i];
  67. lowestMonth = i;
  68. }
  69. }
  70. average = totalRainfall / MONTHS;
  71.  
  72. //Display Results
  73. cout << "RAINFALL STATISTICS: " << endl;
  74. cout << "Total rainfall for the year: " << totalRainfall << " inches" << endl;
  75. cout << "Average monthly rainfall: " << average << " inches" << endl;
  76. cout << "Month with highest rainfall: " << monthNames[highestMonth] << " (";
  77. cout << highest << " inches)" << endl;
  78. cout << "Month with lowest rainfall: " << monthNames[lowestMonth] << " (";
  79. cout << lowest << " inches)" << endl;
  80. return 0;
  81. }
Success #stdin #stdout 0.01s 5320KB
stdin
12.2
4.4
6.7
8.9
15.5
7.6
8.0
9.4
10.2
11.7
11.3
10.8
stdout
Enter the total rainfall (in inches) for each month: 
January: 
February: 
March: 
April: 
May: 
June: 
July: 
August: 
September: 
October: 
November: 
December: 
RAINFALL STATISTICS: 
Total rainfall for the year: 116.7 inches
Average monthly rainfall: 9.725 inches
Month with highest rainfall: May (15.5 inches)
Month with lowest rainfall: February (4.4 inches)