Advertisement
juliomares67

C++ Example 2

Dec 8th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include<iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. const int NUM_MONTHS = 12;
  6.  
  7. double getTotal(double[]);
  8. double getAverage(double[]);
  9. double getLargest(double[], int &);
  10. double getSmallest(double[], int &);
  11.  
  12.  
  13. int main()
  14. {
  15.     double rainFall[NUM_MONTHS];
  16.  
  17.     for (int month = 0; month < NUM_MONTHS; month++)
  18.     {
  19.         cout << "Enter the rainfall for month ";
  20.         cout << (month + 1) << ": ";
  21.         cin >> rainFall[month];
  22.  
  23.         while (rainFall[month] < 0)
  24.         {
  25.             cout << "Rainfall must be 0 or more.\n"
  26.                 << "Please re-enter: ";
  27.             cin >> rainFall[month];
  28.         }
  29.     }
  30.  
  31.     cout << fixed << showpoint << setprecision(2) << endl;
  32.  
  33.     cout << "The total rainfall for the year is ";
  34.     cout << getTotal(rainFall)
  35.         << " inches." << endl;
  36.  
  37.     cout << "The average rainfall for the year is ";
  38.     cout << getAverage(rainFall)
  39.         << " inches." << endl;
  40.  
  41.     int subScript = 0;
  42.  
  43.     cout << "The largest amount of rainfall was ";
  44.     cout << getLargest(rainFall, subScript)
  45.         << " inches in month ";
  46.     cout << (subScript + 1) << "." << endl;
  47.  
  48.     cout << "The smallest amount of rainfall was ";
  49.     cout << getSmallest(rainFall, subScript)
  50.         << " inches in month ";
  51.     cout << (subScript + 1) << "." << endl << endl;
  52.    
  53.     return 0;
  54. }
  55.  
  56. double getTotal(double rainFall[])
  57. {
  58.     double total = 0;
  59.     for (int count = 0; count < NUM_MONTHS; count++)
  60.         total += rainFall[count];
  61.     return total;
  62. }
  63.  
  64. double getAverage(double rainFall[])
  65. {
  66.     double average = 0.0;
  67.     average = getTotal(rainFall) / NUM_MONTHS;
  68.     return average;
  69. }
  70.  
  71. double getLargest(double rainFall[], int &subscript)
  72. {
  73.     double largest;
  74.     largest = rainFall[0];
  75.  
  76.     for (int month = 0; month < NUM_MONTHS; month++){
  77.         if (rainFall[month] > largest){
  78.             largest = rainFall[month];
  79.             subscript = month;
  80.         }
  81.     }
  82.  
  83.     return largest;
  84. }
  85.  
  86. double getSmallest(double rainFall[], int &subscript)
  87. {
  88.     double smallest;
  89.     smallest = rainFall[0];
  90.  
  91.     for (int month = 0; month < NUM_MONTHS; month++){
  92.         if (rainFall[month] < smallest){
  93.             smallest = rainFall[month];
  94.             subscript = month;
  95.         }
  96.     }
  97.     return smallest;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement