Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. //Prototypes go here
  9. double calc(int rainfall[], int totalRainfall, double averageRainfall);
  10. void output(int totalRainall, double averageRainfall, int rainHigh, int rainLow, string months[], string monthHigh, string monthLow);
  11.  
  12. int main()
  13. {
  14.     //Variables go here
  15.     string months[12] = {"January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  16.     string monthHigh = " ";
  17.     string monthLow = " ";
  18.     char error_ch = ' ';
  19.     char again = ' ';
  20.  
  21.     int rainfall[12] = {0};
  22.     int rainHigh = 0;
  23.     int rainLow = 500000;
  24.     int totalRainfall = 0;
  25.     double averageRainfall = 0;
  26.  
  27.     for(int i = 0; i < 12; i ++)
  28.     {
  29.         cout << "How much rain was collected in " << months[i] << "?" << endl;
  30.         cin >> rainfall[i];
  31.  
  32.         if(cin.fail())
  33.         {
  34.             cin.clear();
  35.             cin >> error_ch;
  36.             cout << endl << "You used a letter please use a number instead." << endl;
  37.             cout << endl;
  38.             cout << "Repeat enter." << endl;
  39.             if(i == 0)
  40.             {
  41.                 main();
  42.             }
  43.             else if(i > 0)
  44.             {
  45.                 i--;
  46.             }
  47.         }
  48.  
  49.         if(rainfall[i] > rainHigh)
  50.         {
  51.             rainHigh = rainfall[i];
  52.             monthHigh = months[i];
  53.         }
  54.         if(rainfall[i] < rainLow)
  55.         {
  56.             rainLow = rainfall[i];
  57.             monthLow = months[i];
  58.         }
  59.     }
  60.     averageRainfall = calc(rainfall, totalRainfall, averageRainfall);
  61.     output(totalRainfall, averageRainfall, rainHigh, rainLow, months, monthHigh, monthLow);
  62.  
  63.     system("Pause");
  64.     system("cls");
  65.  
  66.     //Runs the program again
  67.     cout << "Would you like to run this program again? y or n: " << endl;
  68.     cin >> again;
  69.     again = tolower(again);
  70.    
  71.     if(again == 'y')
  72.     {
  73.         system("cls");
  74.         main();
  75.     }
  76.     else
  77.     {
  78.         return 0;
  79.     }
  80. }
  81.  
  82. double calc(int rainfall[], int totalRainfall, double averageRainfall)
  83. {
  84.     for(int i = 0; i < 12; i ++)
  85.     {
  86.         totalRainfall = rainfall[i] + totalRainfall;
  87.     }
  88.  
  89.     averageRainfall = totalRainfall / 12;
  90.  
  91.     return averageRainfall;
  92. }
  93.  
  94. void output(int totalRainfall, double averageRainfall, int rainHigh, int rainLow, string months[], string monthHigh, string monthLow)
  95. {
  96.     cout << endl;
  97.    
  98.     cout << "Over the 12 months recorded the average rainfall for the year was " << averageRainfall << "." << endl << endl;
  99.  
  100.     cout << "Over the 12 month period the highest recorded rainfall is: " << rainHigh << endl << " which was during the month of " << monthHigh << "." << endl << endl;
  101.  
  102.     cout << "Over the 12 month period the lowest recorded rainfall is: " << rainLow << endl << "  which was during the month of" << monthLow << "." << endl << endl;
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement