fork download
  1. //Sam Partovi CS1A Chapter 3, P. 143, #4
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE AVERAGE RAINFALL
  6.  * ____________________________________________________________
  7.  * This program computes the average rainfall of 3 months based on given
  8.  * rainfall measurements for each month.
  9.  *
  10.  * Average rainfall is calculated with the following formula:
  11.  * avgRainfall = ( firstRainfall + secondRainfall + thirdRainfall ) / monthCount
  12.  * ____________________________________________________________
  13.  *INPUT
  14.  * firstMonthName : Name of the first month
  15.  * secondMonthName : Name of the second month
  16.  * thirdMonthName : Name of the third month
  17.  * firstRainfall : Rainfall measurement of the first month (in)
  18.  * secondRainfall : Rainfall measurement of the second month (in)
  19.  * thirdRainfall : Rainfall measurement of the third month (in)
  20.  * monthCount : Number of months to calculate average
  21.  *
  22.  *OUTPUT
  23.  * avgRainfall : The average rainfall in inches
  24.  *
  25.  ******************************************************************************/
  26.  
  27. #include <iostream>
  28. #include <iomanip>
  29. using namespace std;
  30. int main ()
  31. {
  32. //Variable dictionary
  33. string firstMonthName; //INPUT - Name of the first month
  34. string secondMonthName; //INPUT - Name of the second month
  35. string thirdMonthName; //INPUT - Name of the third month
  36. float firstRainfall; //INPUT - Rainfall measurement of the first month (in)
  37. float secondRainfall; //INPUT - Rainfall measurement of the second month (in)
  38. float thirdRainfall; //INPUT - Rainfall measurement of the third month (in)
  39. int monthCount; //INPUT - Number of months to calculate average
  40. float avgRainfall; //OUTPUT - The average rainfall in inches
  41.  
  42. //Initialize program variables
  43. monthCount = 3;
  44.  
  45. //Prompt user for month names
  46. cout << "First month name: ";
  47. cin >> firstMonthName;
  48. cout << "Second month name: ";
  49. cin >> secondMonthName;
  50. cout << "Third month name: \n";
  51. cin >> thirdMonthName;
  52.  
  53. //Prompt user for rainfall in inches
  54. cout << "Enter the rainfall (inches) for the month of " << firstMonthName
  55. << ": \n";
  56. cin >> firstRainfall;
  57. cout << "Enter the rainfall (inches) for the month of " << secondMonthName
  58. << ": \n";
  59. cin >> secondRainfall;
  60. cout << "Enter the rainfall (inches) for the month of " << thirdMonthName
  61. << ": \n";
  62. cin >> thirdRainfall;
  63.  
  64. //Compute average rainfall
  65. avgRainfall = (firstRainfall + secondRainfall + thirdRainfall) / monthCount;
  66.  
  67. //Output results
  68. cout << "\nThe average rainfall for the months of " << firstMonthName << ", "
  69. << secondMonthName << ", " << "and " << thirdMonthName << " is " <<
  70. setprecision(4) << avgRainfall << " inches." << endl;
  71.  
  72. return 0;
  73. }
  74.  
  75.  
Success #stdin #stdout 0.01s 5292KB
stdin
January
February
March
12.20
14.00
9.09
stdout
First month name: Second month name: Third month name: 
Enter the rainfall (inches) for the month of January: 
Enter the rainfall (inches) for the month of February: 
Enter the rainfall (inches) for the month of March: 

The average rainfall for the months of January, February, and March is 11.76 inches.