fork download
  1. //Ryan Shahriyarpour CS1A Carl Argila CH7 HW Q:2
  2. /*****************************************************************************
  3.  *
  4.  * Rainfall Statistics
  5.  *
  6.  *********************
  7.  *
  8.  * This program reads the rainfall for each of 12 months, then
  9.  * displays the total rainfall, the average monthly rainfall, and the months
  10.  * with the highest and lowest rainfall
  11.  *
  12.  * INPUT
  13.  * Rainfall (inches) for each of the 12 months
  14.  *
  15.  * OUTPUT
  16.  * Total rainfall, average rainfall, highest month, lowest month
  17.  ****************************************************************************/
  18.  
  19. #include <iostream>
  20. #include <string>
  21. using namespace std;
  22.  
  23. const int MONTHS = 12;
  24.  
  25. int main()
  26. {
  27. string names[MONTHS] = {"January", "February", "March", "April", "May",
  28. "June", "July", "August", "September", "October",
  29. "November", "December"};
  30. double rain[MONTHS];
  31. double total = 0;
  32. double average;
  33. int high = 0;
  34. int low = 0;
  35.  
  36. // Input
  37. for (int i = 0; i < MONTHS; i++)
  38. {
  39. cout << "Enter rainfall for " << names[i] << ": ";
  40. cin >> rain[i];
  41. while (rain[i] < 0)
  42. {
  43. cout << "Cannot be negative. Re-enter: ";
  44. cin >> rain[i];
  45. }
  46. }
  47.  
  48. // Add up the total and find the highest and lowest months
  49. for (int i = 0; i < MONTHS; i++)
  50. {
  51. total = total + rain[i];
  52. if (rain[i] > rain[high])
  53. high = i;
  54. if (rain[i] < rain[low])
  55. low = i;
  56. }
  57. average = total / MONTHS;
  58.  
  59. // Output
  60. cout << "\nTotal rainfall: " << total << endl;
  61. cout << "Average monthly rainfall: " << average << endl;
  62. cout << "Highest month: " << names[high] << " (" << rain[high] << ")" << endl;
  63. cout << "Lowest month: " << names[low] << " (" << rain[low] << ")" << endl;
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Enter rainfall for January: Enter rainfall for February: Enter rainfall for March: Enter rainfall for April: Enter rainfall for May: Enter rainfall for June: Enter rainfall for July: Enter rainfall for August: Enter rainfall for September: Enter rainfall for October: Enter rainfall for November: Enter rainfall for December: 
Total rainfall: 9.88131e-324
Average monthly rainfall: 0
Highest month: January (9.88131e-324)
Lowest month: February (0)