fork download
  1. //Kanoa Holden CS1A Chapter 3, P. 143 Q#4
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE AVERAGE RAINFALL IN INCHES
  6.  * _____________________________________________________________________________
  7.  * This program calculates the average amount of rainfall in certain months in
  8.  * inches.
  9.  *
  10.  * Computation is based on the formula:
  11.  * averageRainfall = (rain1 + rain2 + rain3) / 3.0
  12.  * _____________________________________________________________________________
  13.  * INPUT
  14.  * month1 : name of first month
  15.  * rain1 : amount of rain in first month
  16.  * month2 : name of second month
  17.  * rain2 : amount of rain in second month
  18.  * month3 : name of third month
  19.  * rain3 : amount of rain in third month
  20.  *
  21.  * OUTPUT
  22.  * averageRainfall : average rainfall in the three months
  23.  *
  24.  ******************************************************************************/
  25. #include <iostream>
  26. #include <iomanip>
  27. #include <string>
  28.  
  29. using namespace std;
  30.  
  31. int main() {
  32. // Variables to store the names of the months
  33. string month1, month2, month3;
  34.  
  35. // Variables to store the amount of rainfall for each month
  36. double rain1, rain2, rain3;
  37.  
  38. // Ask the user to input the name and rainfall for each month
  39. cout << "\nEnter the name of the first month: ";
  40. cin >> month1;
  41. cout << "\nEnter the amount of rain (in inches) for " << month1 << ": ";
  42. cin >> rain1;
  43.  
  44. cout << "\nEnter the name of the second month: ";
  45. cin >> month2;
  46. cout << "\nEnter the amount of rain (in inches) for " << month2 << ": ";
  47. cin >> rain2;
  48.  
  49. cout << "\nEnter the name of the third month: ";
  50. cin >> month3;
  51. cout << "\nEnter the amount of rain (in inches) for " << month3 << ": ";
  52. cin >> rain3;
  53.  
  54. // Calculate the average rainfall
  55. double averageRainfall = (rain1 + rain2 + rain3) / 3.0;
  56.  
  57. // Display the average rainfall
  58. cout << fixed << setprecision(2);
  59. cout << "\nThe average rainfall for " << month1 << ", " << month2 << ", and " << month3 << " is " << averageRainfall << " inches." << endl;
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0s 5284KB
stdin
June 
3
May
4
April
3
stdout
Enter the name of the first month: 
Enter the amount of rain (in inches) for June: 
Enter the name of the second month: 
Enter the amount of rain (in inches) for May: 
Enter the name of the third month: 
Enter the amount of rain (in inches) for April: 
The average rainfall for June, May, and April is 3.33 inches.