Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <array>
- /*
- * Created by: Jason Sandoz @ CHC
- * Programming Challenge 2: Rainfall Statistics
- * This program calculates and displays the highest & lowest recorded rainfall for a 12 month period.
- * It also displays the average monthly rainfall in inches.
- *
- * *** Input validation: Users cannot enter negative values for rainfall ***
- */
- // Jason Sandoz
- void getAvg(std::array<double, 12> arr, int SIZE){ // Function to calculate average and total rain fall
- int i = 0;
- double avg, sum = 0;
- for(int i = 0; i < SIZE; i++){
- sum += arr[i];
- }
- avg = (sum / SIZE);
- std::cout << "The Average rainfall is: " << avg << " inches." << std::endl; // Display the average rainfall for the 12 months
- std::cout << "The total rainfall is: " << sum << " inches." << std::endl; // Display the total rainfall for the 12 months
- }
- void getHighest(std::array<double, 12> arr, int SIZE){
- double highestValue = arr[0];
- int highestMonth = 0;
- for(int i = 0; i < SIZE; i++){
- if(highestValue < arr[i]){
- highestValue = arr[i];
- highestMonth = i + 1;
- }
- }
- std::cout << "Month: " << highestMonth << " @ " << highestValue << " inches, is the highest value of rainfall.\n";
- }
- void getLowest(std::array<double, 12> arr, int SIZE){
- double lowestValue = arr[0];
- int lowestMonth = 1;
- for(int i = 0; i < SIZE; i++){
- if(lowestValue > arr[i]){
- lowestValue = arr[i];
- lowestMonth = i + 1;
- }
- }
- std::cout << "Month: " << lowestMonth << " @ " << lowestValue << " inches, is the lowest value of rainfall.\n";
- }
- int main(){
- const int SIZE = 12; // 12 months in a year
- std::array<double, 12> totalRainFall; // create an array to store rainfall values
- for(int i = 0; i < SIZE; i++){ // loop through the array and populate.
- std::cout << "Please input the total rainfall for month: " << i + 1 << " in inches." << std::endl;
- std::cin >> totalRainFall[i];
- if(totalRainFall[i] < 0){ // check for negative input. (evaporation? :D)
- std::cout << "Error: rainfall cannot be negative.\n";
- return 0; // Kill the program if a user enters a negative number;
- }
- }
- getAvg(totalRainFall, SIZE); // Call our function to display averages and sums
- getHighest(totalRainFall, SIZE); // Function call to get Highest rainfall
- getLowest(totalRainFall, SIZE); // Function Call to get Lowest rainfall
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement