Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3.  
  4. // Jason Sandoz
  5.  
  6. void getAvg(double arr[], int SIZE){ // Function to calculate average and total rain fall
  7.     int i = 0;
  8.     double avg, sum = 0;
  9.  
  10.     for(int i = 0; i < SIZE; i++){
  11.         sum += arr[i];
  12.     }
  13.  
  14.     avg = (sum / SIZE);
  15.  
  16.     std::cout << "The Average rainfall is: " << avg << " inches." << std::endl; // Display the average rainfall for the 12 months
  17.     std::cout << "The total rainfall is: " << sum << " inches." << std::endl; // Display the total rainfall for the 12 months
  18. }
  19.  
  20. int main(){
  21.  
  22.     const int SIZE = 12; // 12 months in a year
  23.     std::array<double, 12> totalRainFall; // create array; the problem is here when I use std::array<double, SIZE> totalRainfall;
  24.  
  25.     /* When using std::array I get an error that says "initializing argument 1 of double getAvg(double *, int) cannot convert to double */
  26.  
  27.  
  28.     for(int i = 0; i < SIZE; i++){ // loop through the array and populate.
  29.         std::cout << "Please input the total rainfall for month: " << i + 1 << " in inches." << std::endl;
  30.         std::cin >> totalRainFall[i];
  31.  
  32.         if(totalRainFall[i] < 0){ // check for negative input. (evaporation? :D)
  33.             std::cout << "Error: rainfall cannot be negative.\n";
  34.             return 0;
  35.         }
  36.  
  37.      
  38.        
  39.     }
  40.  
  41.     getAvg(totalRainFall, SIZE); // Call our function to display averages and sums
  42.      
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement