fork download
  1. // Andres Guzman CSC5 Chapter 7, P. 444, #2
  2. /******************************************************************************
  3.  *
  4.  * DISPLAY RAINFALL STATISTICS
  5.  * ____________________________________________________________________________
  6.  * This program will calculate and display the total, average, and highest and
  7.  * lowest months of rainfall.
  8.  *
  9.  * Computation:
  10.  *
  11.  * total / 12
  12.  * ____________________________________________________________________________
  13.  *
  14.  * INPUT
  15.  *
  16.  * [SIZE] = Array Size
  17.  * rainfall[SIZE] = Holds the inputed values of rainfall for each month
  18.  *
  19.  * OUTPUT
  20.  *
  21.  * total = Displays the total accumulated rainfall
  22.  * months[highestmonth] = Displays highest month of rainfall
  23.  * months[lowestmonth] = Display lowest month of rainfall
  24.  *
  25.  ******************************************************************************/
  26. #include <iostream>
  27. #include <iomanip>
  28. #include <string>
  29. using namespace std;
  30.  
  31. int main()
  32. {
  33. const int SIZE = 12; //Array size
  34. string months[SIZE] = {"January", "February", "March", "April", "May", "June",
  35. "July", "August", "September", "October", "November",
  36. "December"}; //Initialization List for month
  37. double rainfall[SIZE]; //Subscript to input values for rainfall
  38. double total = 0; //Accumulator for rainfall
  39. int i; //Cycles through subscript in for loops
  40. //
  41. //Initializing For Loop
  42. for(i = 0; i < SIZE; i++)
  43. {
  44. cout << months[i] << " Rainfall: ";
  45. cin >> rainfall[i];
  46. cout << endl;
  47. while(rainfall[i] < 0) //Input Validiation
  48. {
  49. cout << "Invalid Rainfall Amount, Please Try Again." << endl;
  50. cout << months[i] << " Rainfall: ";
  51. cin >> rainfall[i];
  52. cout << endl;
  53. }
  54. total += rainfall[i]; //Adds rainfall input to total
  55. }
  56. double highest = rainfall[0]; //Assigns beginning of subscript to highest
  57. double lowest = rainfall [0]; //Assigns beginning of subscript to lowest
  58. int highestmonth = 0; //Assigns variable to subscript as a name
  59. int lowestmonth = 0; //Assigns variable to subscript as a name
  60. //
  61. //Initializing For Loop
  62. for(i = 0; i < SIZE; i++)
  63. {
  64. if(rainfall[i] > highest) //Finds which subscript is bigger than [0]
  65. {
  66. highest = rainfall[i]; //Assigns found subscript to highest
  67. highestmonth = i; //Assigns highestmonth to subscript [0]
  68.  
  69. }
  70. if(rainfall[i] < lowest) //Finds which subscirpt is lower than [0]
  71. {
  72. lowest = rainfall[i]; //Assigns found subscript to lower
  73. lowestmonth = i; //Assigns lowestmonth to subscript [0]
  74. }
  75. }
  76. cout << "\nTotal Rainfall: " << total << endl;
  77. cout << fixed << setprecision(2); //Allows 2 decimal points for average
  78. cout << "Average Monthly Rainfall: " << (total / 12) << endl;
  79. cout << "Highest Month of Rainfall: " << months[highestmonth] << endl;
  80. cout << "Lowest Month of Rainfall: " << months[lowestmonth] << endl;
  81. return 0;
  82. }
Success #stdin #stdout 0.01s 5324KB
stdin
12.6 30.2 20 20 20 2 2 2 2 2 2 2
stdout
January Rainfall: 
February Rainfall: 
March Rainfall: 
April Rainfall: 
May Rainfall: 
June Rainfall: 
July Rainfall: 
August Rainfall: 
September Rainfall: 
October Rainfall: 
November Rainfall: 
December Rainfall: 

Total Rainfall: 116.8
Average Monthly Rainfall: 9.7
Highest Month of Rainfall: February
Lowest Month of Rainfall: June