fork download
  1. //Devin Scheu CS1A Chapter 3, P. 143, #4
  2. //
  3. /**************************************************************
  4. *
  5. * CALCULATE AVERAGE RAINFALL
  6. * ____________________________________________________________
  7. * This program calculates the average rainfall for three months
  8. * based on month names and rainfall amounts.
  9. *
  10. * Computation is based on the formula:
  11. * average = (rainfall1 + rainfall2 + rainfall3) / 3
  12. * ____________________________________________________________
  13. * INPUT
  14. * month1 : Name of the first month (as a string)
  15. * month2 : Name of the second month (as a string)
  16. * month3 : Name of the third month (as a string)
  17. * rainfall1 : Rainfall for the first month (in inches, as a double)
  18. * rainfall2 : Rainfall for the second month (in inches, as a double)
  19. * rainfall3 : Rainfall for the third month (in inches, as a double)
  20. *
  21. * OUTPUT
  22. * average : The average rainfall for the three months (in inches)
  23. *
  24. **************************************************************/
  25.  
  26. #include <iostream>
  27. #include <iomanip>
  28. #include <string>
  29.  
  30. using namespace std;
  31.  
  32. int main () {
  33.  
  34. //Variable Declarations
  35. string month1; //INPUT - Name of the first month (as a string)
  36. string month2; //INPUT - Name of the second month (as a string)
  37. string month3; //INPUT - Name of the third month (as a string)
  38. float rainfall1; //INPUT - Rainfall for the first month (in inches, as a float)
  39. float rainfall2; //INPUT - Rainfall for the second month (in inches, as a double)
  40. float rainfall3; //INPUT - Rainfall for the third month (in inches, as a double)
  41. float sum; //PROCESSING - The sum of the rainfall amounts
  42. float average; //OUTPUT - The average rainfall for the three months (in inches)
  43.  
  44. //Prompt for Input
  45. cout << "Enter the name of the first month: ";
  46. getline(cin, month1);
  47. cout << "\nEnter the rainfall for " << month1 << " (in inches): ";
  48. cin >> rainfall1;
  49. cin.ignore(); //Clear input buffer
  50. cout << "\nEnter the name of the second month: ";
  51. getline(cin, month2);
  52. cout << "\nEnter the rainfall for " << month2 << " (in inches): ";
  53. cin >> rainfall2;
  54. cin.ignore(); //Clear input buffer
  55. cout << "\nEnter the name of the third month: ";
  56. getline(cin, month3);
  57. cout << "\nEnter the rainfall for " << month3 << " (in inches): ";
  58. cin >> rainfall3;
  59. cin.ignore(); //Clear input buffer
  60. cout << endl << "OUTPUT: " << endl;
  61.  
  62. //Calculate Sum
  63. sum = rainfall1 + rainfall2 + rainfall3;
  64.  
  65. //Calculate Average
  66. average = sum / 3.0;
  67.  
  68. //Output Result
  69. cout << fixed << setprecision(2) << "The rainfall for "<< month1 <<" is: " << rainfall1 << " inches." << endl;
  70. cout << fixed << setprecision(2) << "The rainfall for "<< month2 <<" is: " << rainfall2 << " inches." << endl;
  71. cout << fixed << setprecision(2) << "The rainfall for "<< month3 <<" is: " << rainfall3 << " inches.";
  72. cout << fixed << setprecision(2) << "\nThe average rainfall for " << month1 << ", " << month2 << ", and " << month3 << " is " << average << " inches." << endl;
  73.  
  74. } //end of main()
Success #stdin #stdout 0.01s 5292KB
stdin
May
10
June
5
July
7.5
stdout
Enter the name of the first month: 
Enter the rainfall for May (in inches): 
Enter the name of the second month: 
Enter the rainfall for June (in inches): 
Enter the name of the third month: 
Enter the rainfall for July (in inches): 
OUTPUT: 
The rainfall for May is: 10.00 inches.
The rainfall for June is: 5.00 inches.
The rainfall for July is: 7.50 inches.
The average rainfall for May, June, and July is 7.50 inches.