fork download
  1. //Jacklyn Isordia CSC5 Chapter 3, P. 143, #04
  2. //
  3. /**************************************************************
  4.  *
  5.  * Calculate Average Rainfall
  6.  * ____________________________________________________________
  7.  * This program calculates the average rainfall for three months. The user
  8.  * enters the name of each month and the rainfall amount (in inches) for each
  9.  * month. The program then computes the average rainfall
  10.  
  11.  * Computation is based on the formula:
  12.  * Average Rainfall = (Rain1 + Rain2 + Rain3) / 3
  13.  * ____________________________________________________________
  14.  * INPUT
  15.  * month1 : name of first month
  16.  * month2 : name of second month
  17.  * month3 : name of third month
  18.  * rain1 : rainfall for month1 (in inches)
  19.  * rain2 : rainfall for month2 (in inches)
  20.  * rain3 : rainfall for month3 (in inches)
  21.  *
  22.  * OUTPUT
  23.  * averageRain : average rainfall for the three months
  24.  *
  25.  **************************************************************/
  26. #include <iostream>
  27. #include <string>
  28. using namespace std;
  29.  
  30. int main ()
  31. {
  32. string month1;
  33. string month2;
  34. string month3;
  35.  
  36. float rain1;
  37. float rain2;
  38. float rain3;
  39.  
  40. float averageRain;
  41. //
  42. // Input Values
  43. //
  44. cout << "Enter the first month: " << endl;
  45. cin >> month1;
  46.  
  47. cout << "Enter the second month: " << endl;
  48. cin >> month2;
  49.  
  50. cout << "Enter the third month: " << endl;
  51. cin >> month3;
  52.  
  53. cout << "Enter rainfall (in inches) for " << month1 << ": " << endl;
  54. cin >> rain1;
  55.  
  56. cout << "Enter rainfall (in inches) for " << month2 << ": " << endl;
  57. cin >> rain2;
  58.  
  59. cout << "Enter rainfall (in inches) for " << month3 << ": " << endl;
  60. cin >> rain3;
  61. //
  62. // Compute Average Rainfall
  63. //
  64. averageRain = (rain1 + rain2 + rain3) / 3;
  65. //
  66. // Output Result
  67. //
  68. cout << "The average rainfall for "
  69. << month1 << ", " << month2 << ", and " << month3
  70. << " is " << averageRain << " inches." << endl;
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 5284KB
stdin
August 
September
October
5.5
7.2
7.46
stdout
Enter the first month: 
Enter the second month: 
Enter the third month: 
Enter rainfall (in inches) for August: 
Enter rainfall (in inches) for September: 
Enter rainfall (in inches) for October: 
The average rainfall for August, September, and October is 6.72 inches.