fork download
  1. //Cenyao Huang CS1A Homework 3, P. 143, #4
  2.  
  3. /*********************************************************************************
  4. * COMPUTE AVERAGE RAINFALL
  5. * This program computes the average rainfall for three months.
  6. *
  7. * This program will use the formula:
  8. * average = (rainfall_month1 + rainfall_month2 + rainfall_month3)/3
  9. *
  10. * Input
  11. * month1 : the name of a month
  12. * month2 : the name of another month
  13. * month3 : the name of a different month
  14. * rainfall_month1 : the amount of rainfall for month1
  15. * rainfall_month2 : the amount of rainfall for month2
  16. * rainfall_month3 : the amount of rainfall for month3
  17. *
  18. * Output
  19. * average : the average of rainfall_month1, rainfall_month2, and rainfall_month3
  20. *
  21. *********************************************************************************/
  22.  
  23. #include <iostream>
  24. #include <iomanip>
  25. #include <string>
  26. using namespace std;
  27.  
  28. int main() {
  29. string month1, month2, month3; // assign data type to variables
  30. double rainfall_month1, rainfall_month2, rainfall_month3, average;
  31.  
  32. cout << "Please provide the names of three months: "; // get the names of the months
  33. cin >> month1 >> month2 >> month3;
  34. cout << month1 << " " << month2 << " " << month3 << endl;
  35.  
  36. cout << "Please provide the rainfall of each month respectively: "; // get the amount of rainfall of each month
  37. cin >> rainfall_month1 >> rainfall_month2 >> rainfall_month3;
  38. cout << rainfall_month1 << " " << rainfall_month2 << " " << rainfall_month3 << endl;
  39.  
  40. average = (rainfall_month1 + rainfall_month2 + rainfall_month3)/3; // compute and display average
  41. cout << "The average of rainfall for " << month1 << ", " << month2 << ", and " << month3 << " is "
  42. << fixed << setprecision(2) << average << " inches";
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 5320KB
stdin
June July August 2.0 3.75 4.0
stdout
Please provide the names of three months: June July August
Please provide the rainfall of each month respectively: 2 3.75 4
The average of rainfall for June, July, and August is 3.25 inches