fork download
  1. // Timothy Price CS1A Ch 7 Challenge 2
  2. /*******************************************************************************
  3.  * Calculate rainfall
  4.  *
  5.  * This program receives a number of values for monthly rainfall, and calculates
  6.  * an annual total, monthly average, and least and greatest.
  7.  *
  8.  * Inputs:
  9.  * Rainfall for each month
  10.  *
  11.  * Outputs:
  12.  * Annual rainfall
  13.  * Monthly average rainfall
  14.  * Least month
  15.  * Greatest month
  16.  ******************************************************************************/
  17.  
  18. #include <iostream>
  19. #include <cmath> // for calculations
  20. #include <iomanip> // for fixed decimal points
  21.  
  22. using namespace std;
  23.  
  24. // established number of months
  25. const int SIZE = 12;
  26.  
  27. int main() {
  28.  
  29. // data dictionary
  30. double month[SIZE]; // array to gather each month's rainfall
  31. double annual; // annual total
  32. double average; // monthly average
  33. float greatest; // month with greatest rainfall
  34. float least; // month with least rainfall
  35.  
  36. // loop to receive inputs
  37. for(int i = 0; i < SIZE; i++){
  38. cout << "Enter the total rainfall for the month: ";
  39. cin >> month[i];
  40. cout << month[i] << endl;
  41.  
  42. // input validation
  43. if(month[i] <= 0){
  44. cout << "Please enter a positive number: ";
  45. cin >> month[i];
  46. cout << month[i] << endl;
  47. }
  48.  
  49. // add each month value to annual total
  50. annual += month[i];
  51. }
  52.  
  53. // set starting values for greatest and least
  54. greatest = month[0];
  55. least = month[0];
  56.  
  57. // loop to determine greatest or least
  58. for(int i = 0; i < SIZE; i++){
  59. if(month[i] > greatest)
  60. greatest = month[i];
  61. else if(month[i] < least)
  62. least = month[i];
  63. }
  64.  
  65. // calculate monthly average
  66. average = (annual / SIZE);
  67.  
  68. // display results
  69. cout << fixed << setprecision(2);
  70. cout << "--------------- RESULTS ---------------" << endl;
  71. cout << "Annual total rainfall: " << annual << endl;
  72. cout << "Average monthly rainfall: " << average << endl;
  73. cout << "Greatest monthly rainfall: " << greatest << endl;
  74. cout << "Least monthly rainfall: " << least << endl;
  75. return 0;
  76. }
Success #stdin #stdout 0s 5320KB
stdin
12
14
16
24
40
13.9
15.4
10.01
1.11
20.3
11.4
18.5
stdout
Enter the total rainfall for the month: 12
Enter the total rainfall for the month: 14
Enter the total rainfall for the month: 16
Enter the total rainfall for the month: 24
Enter the total rainfall for the month: 40
Enter the total rainfall for the month: 13.9
Enter the total rainfall for the month: 15.4
Enter the total rainfall for the month: 10.01
Enter the total rainfall for the month: 1.11
Enter the total rainfall for the month: 20.3
Enter the total rainfall for the month: 11.4
Enter the total rainfall for the month: 18.5
--------------- RESULTS ---------------
Annual total rainfall: 196.62
Average monthly rainfall: 16.39
Greatest monthly rainfall: 40.00
Least monthly rainfall: 1.11