fork download
  1. //Xinnuo Wang CS1A chapter 5 , P.295, #10
  2. //
  3. /*******************************************************************************
  4. *
  5. * Compute average rainfall
  6. *-------------------------------------------------------------------------------
  7. * This program accepts data and compute the average rainfall over a period of
  8. * years.
  9. *
  10. * computation is based on the formula:
  11. * Average rainfall per month = total rainfall / (The number of years x 12)
  12. *-------------------------------------------------------------------------------
  13. * INPUT
  14. * The number of years : The asked number of years
  15. * The number of month : The number of that month
  16. * Inches of rainfall in the month: The asked inches in that month
  17. * OUTPUT
  18. * Total inches : Total inches of rainfall
  19. * Average rainfall per month : The average inches of rainfall per month
  20. *******************************************************************************/
  21. #include <iostream>
  22. using namespace std;
  23.  
  24. int main()
  25. {
  26. int numbOfMonth; //INPUT - The asked inches in that month
  27. int numbOfYear; //INPUT
  28. int numbOfYears; //INPUT - The asked number of years
  29. double inchesInTheMonth; // INPUT - The asked inches in that month
  30. double totalInches; //OUTPUT - Total inches of rainfall
  31. double avePerMonth; // OUTPUT - The average inches of rainfall per month
  32. cout <<"Enter time in years"<< endl;
  33. cin>> numbOfYears;
  34. //
  35. //Input Validation
  36. while(numbOfYears< 1)
  37. {
  38. cout<<"Invalid input"<<endl;
  39. return 0;
  40. }
  41. //
  42. //Initialization
  43. numbOfYear = 0;
  44. totalInches = 0;
  45. //
  46. //Process
  47. while (numbOfYear<numbOfYears)
  48. {
  49. numbOfMonth = 0;
  50. while(numbOfMonth < 12)
  51. {
  52. cout<< "The rainfall in month "<< numbOfMonth<<" is : "<<endl;
  53. cin >> inchesInTheMonth;
  54. numbOfMonth++;
  55. totalInches+=inchesInTheMonth;
  56. }
  57. numbOfYear++;
  58. }
  59. avePerMonth = totalInches / (numbOfYears*12);
  60. while (inchesInTheMonth< 0)
  61. {
  62. cout<< "Invalid Input" << endl;
  63. return 0;
  64. }
  65. //
  66. //Output Result
  67. cout << "The number of monthes is: " << (numbOfYears*12)<< endl;
  68. cout<< "The total inches of rainfall is: " << totalInches << endl;
  69. cout <<"The average rainfall per month is: "<< avePerMonth << endl;
  70. return 0;
  71. }
Success #stdin #stdout 0s 5312KB
stdin
2 12 12 12 1 1 1 1 1 1.2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
stdout
Enter time in years
The rainfall in month 0 is : 
The rainfall in month 1 is : 
The rainfall in month 2 is : 
The rainfall in month 3 is : 
The rainfall in month 4 is : 
The rainfall in month 5 is : 
The rainfall in month 6 is : 
The rainfall in month 7 is : 
The rainfall in month 8 is : 
The rainfall in month 9 is : 
The rainfall in month 10 is : 
The rainfall in month 11 is : 
The rainfall in month 0 is : 
The rainfall in month 1 is : 
The rainfall in month 2 is : 
The rainfall in month 3 is : 
The rainfall in month 4 is : 
The rainfall in month 5 is : 
The rainfall in month 6 is : 
The rainfall in month 7 is : 
The rainfall in month 8 is : 
The rainfall in month 9 is : 
The rainfall in month 10 is : 
The rainfall in month 11 is : 
The number of monthes is: 24
The total inches of rainfall is: 57.2
The average rainfall per month is: 2.38333