fork download
  1. //Jonathan Estrada CSC5 Chapter 7, P.444, #2
  2. /*******************************************************************************
  3.  * COMPUTE RAINFALL
  4.  * _____________________________________________________________________________
  5.  * This program asks user for rain fall for each month but not accepting invalid
  6.  * for all 12 months in the year. The total rainfall for the year is acumulated
  7.  * and then used to compute the monthly average. By the end the program will
  8.  * display which month had the most and least rainfall throughout the year.
  9.  * _____________________________________________________________________________
  10.  * INPUT
  11.  * rainfall : number of months and number of inputs
  12.  *
  13.  * OUTPUT
  14.  * totalRainAmmount : accumulated rain fall
  15.  * monthlyavgRainfall : calucated average monthly rainfall
  16.  * highest : highest rainfall
  17.  * lowest : lowest rainfall
  18.  *
  19.  * *****************************************************************************/
  20. #include <iostream>
  21. #include <iomanip>
  22. using namespace std;
  23.  
  24. int main() {
  25.  
  26. double rainfall[12];
  27. float totalRainAmmount;
  28. float monthlyavgRainFall;
  29. float highest;
  30. float lowest;
  31.  
  32. cout << "Please enter monthly rainfall for each month," << endl << endl;
  33.  
  34. for(int i = 0; i < 12; i++)
  35. {
  36. cout << "Enter rainfall for month #" << (i+1) << ":";
  37. cin >> rainfall[i];
  38. cout << rainfall[i] << endl;
  39. while(rainfall[i] < 0)
  40. {
  41. cout << "Invalid cannot be negative Please enter rainfall for "
  42. << "month #" << (i+1) << ":";
  43. cin >> rainfall[i];
  44. cout << rainfall[i] << endl;
  45.  
  46. }
  47. }
  48. for(int i = 0; i < 12; i++)
  49. {
  50. totalRainAmmount += rainfall[i];
  51. }
  52.  
  53. highest = rainfall[0];
  54. lowest = rainfall[0];
  55.  
  56. for(int i = 0; i < 12; i++)
  57. {
  58. if(rainfall[i] < lowest)
  59. lowest = rainfall[i];
  60.  
  61. if(rainfall[i] > highest)
  62. highest = rainfall[i];
  63. }
  64. monthlyavgRainFall = totalRainAmmount / 12;
  65. cout << fixed << setprecision(2);
  66. cout << "The average Monthly Rain Fall is " << monthlyavgRainFall
  67. << ", the total rain ammount for the year is " << totalRainAmmount
  68. << " and the highest rainfall was "<< highest << " and "
  69. << "the lowest was " << lowest << "." << endl;
  70.  
  71. return 0;
  72. }
  73.  
Success #stdin #stdout 0.01s 5292KB
stdin
0
5.5
10.5
15.2
8.0
-1
9.6
4.5
5.6
2.3
7.4
8.9
10.7
stdout
Please enter monthly rainfall for each month,

Enter rainfall for month #1:0
Enter rainfall for month #2:5.5
Enter rainfall for month #3:10.5
Enter rainfall for month #4:15.2
Enter rainfall for month #5:8
Enter rainfall for month #6:-1
Invalid cannot be negative Please enter rainfall for month #6:9.6
Enter rainfall for month #7:4.5
Enter rainfall for month #8:5.6
Enter rainfall for month #9:2.3
Enter rainfall for month #10:7.4
Enter rainfall for month #11:8.9
Enter rainfall for month #12:10.7
The average Monthly Rain Fall is 7.35, the total rain ammount for the year is 88.20 and the highest rainfall was 15.20 and the lowest was 0.00.