Advertisement
axyd

Lab 7, part 1

May 4th, 2016
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. void err(double);
  6. void yrRain(double [], const int, double&);
  7. void avgRain(double[], const int, double&);
  8. void hlMonths(double [], const int, double&, double&, int&, int&);
  9.  
  10. int main(){
  11.     const int months = 12;
  12.     char again = 'y';
  13.  
  14.     while (again == 'y' || again == 'Y'){
  15.         double arrain[months];
  16.         cout << "\t\tEnter the rainfall amount for 12 months.\n\n";
  17.  
  18.         for (int i = 0; i < months; i++){
  19.             cout << "Month " << i + 1 <<": ";
  20.             cin >> arrain[i];
  21.  
  22.             while (cin.fail() || arrain[i] < 1){ //fix letter and negatives
  23.                 err(arrain[i]);
  24.                 cout << "Month " << i + 1 << ": ";
  25.                 cin >> arrain[i];
  26.             }
  27.         }
  28.  
  29.         double ttl = 0;
  30.         yrRain(arrain, months, ttl); //yearly rainfall
  31.         cout << "\n\nThe yearly amount of rainfall was: " << setprecision(2) << fixed << ttl << endl;
  32.  
  33.         double avgIn = 0;
  34.         avgRain(arrain, months, avgIn); //monthly rainfall
  35.         cout << "The Average rainfall per month was: " << setprecision(2) << fixed << avgIn << endl;
  36.  
  37.         double maxR = 0, minR = 0;
  38.         int maxM = 0, minM = 0;
  39.         hlMonths(arrain, months, maxR, minR, minM, maxM); //months with highest and lowest rainfall
  40.         cout << "Month " << (minM+1) << " had the lowest rainfall, and it was: " << setprecision(2) << fixed << minR << endl
  41.             << "Month " << (maxM+1) << " had the highest rainfall, and it was: " << setprecision(2) << fixed << maxR << endl << endl;
  42.        
  43.         cout << "\nEnter y/Y to go again: ";
  44.         cin >> again;
  45.     }
  46. }
  47.  
  48. void err(double x){ //fix letter or negative number
  49.     if (cin.fail())
  50.         cout << "\t\t/!\\ Not a number /!\\\n";
  51.     else if (x < 1)
  52.         cout << "\t\t/!\\ Negative number /!\\\n";
  53.     cin.clear();
  54.     fflush(stdin);
  55. }
  56.  
  57. void yrRain(double rainray[], const int m, double &sum){
  58.     for (int i = 0; i < m; i++)
  59.         sum += rainray[i]; 
  60. }
  61.  
  62. void avgRain(double rainray[], const int m, double & avgOut){
  63.     for (int j = 0; j < m; j++)
  64.         avgOut += rainray[j];
  65.     avgOut /= m;
  66. }
  67.  
  68. void hlMonths(double rainray[], const int m, double& maxRain, double& minRain, int& minMonth, int& maxMonth){
  69.     maxRain = rainray[0];
  70.     minRain = rainray[0];
  71.     double tempRain = 0;
  72.  
  73.     for (int k = 0; k < m; k++){
  74.         tempRain = rainray[k];
  75.  
  76.         if (tempRain < minRain){
  77.             minRain = tempRain;
  78.             minMonth = k;
  79.         }
  80.  
  81.         if (tempRain > maxRain){
  82.             maxRain = tempRain;
  83.             maxMonth = k;
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement