Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3.  
  4. /*
  5.  * Created by: Jason Sandoz @ CHC
  6.  * Programming Challenge 2: Rainfall Statistics
  7.  * This program calculates and displays the highest & lowest recorded rainfall for a 12 month period.
  8.  * It also displays the average monthly rainfall in inches.
  9.  *
  10.  * *** Input validation: Users cannot enter negative values for rainfall ***
  11. */
  12.  
  13. // Jason Sandoz
  14.  
  15. void getAvg(std::array<double, 12> arr, int SIZE){ // Function to calculate average and total rain fall
  16.     int i = 0;
  17.     double avg, sum = 0;
  18.  
  19.     for(int i = 0; i < SIZE; i++){
  20.         sum += arr[i];
  21.     }
  22.  
  23.     avg = (sum / SIZE);
  24.  
  25.     std::cout << "The Average rainfall is: " << avg << " inches." << std::endl; // Display the average rainfall for the 12 months
  26.     std::cout << "The total rainfall is: " << sum << " inches." << std::endl; // Display the total rainfall for the 12 months
  27. }
  28.  
  29. void getHighest(std::array<double, 12> arr, int SIZE){
  30.     double highestValue = arr[0];
  31.     int highestMonth = 0;
  32.  
  33.     for(int i = 0; i < SIZE; i++){
  34.         if(highestValue < arr[i]){
  35.             highestValue = arr[i];
  36.             highestMonth = i + 1;
  37.         }
  38.     }
  39.          std::cout << "Month: " << highestMonth << " @ " <<  highestValue << " inches, is the highest value of rainfall.\n";
  40. }
  41.  
  42. void getLowest(std::array<double, 12> arr, int SIZE){
  43.     double lowestValue = arr[0];
  44.     int lowestMonth = 1;
  45.  
  46.     for(int i = 0; i < SIZE; i++){
  47.         if(lowestValue > arr[i]){
  48.             lowestValue = arr[i];
  49.             lowestMonth = i + 1;
  50.         }
  51.     }
  52.          std::cout << "Month: " << lowestMonth  << " @ " <<  lowestValue << " inches, is the lowest value of rainfall.\n";
  53. }
  54.  
  55. int main(){
  56.  
  57.     const int SIZE = 12; // 12 months in a year
  58.     std::array<double, 12> totalRainFall; // create an array to store rainfall values
  59.  
  60.     for(int i = 0; i < SIZE; i++){ // loop through the array and populate.
  61.         std::cout << "Please input the total rainfall for month: " << i + 1 << " in inches." << std::endl;
  62.         std::cin >> totalRainFall[i];
  63.  
  64.         if(totalRainFall[i] < 0){ // check for negative input. (evaporation? :D)
  65.             std::cout << "Error: rainfall cannot be negative.\n";
  66.             return 0; // Kill the program if a user enters a negative number;
  67.         }
  68.  
  69.      
  70.        
  71.     }
  72.  
  73.     getAvg(totalRainFall, SIZE); // Call our function to display averages and sums
  74.     getHighest(totalRainFall, SIZE);  // Function call to get Highest rainfall
  75.     getLowest(totalRainFall, SIZE); // Function Call to get Lowest rainfall
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement