Skip to content

Instantly share code, notes, and snippets.

@Elsayegh
Created August 7, 2018 01:10
Show Gist options
  • Select an option

  • Save Elsayegh/62b1e51b9b1c508daa803d28f82150c1 to your computer and use it in GitHub Desktop.

Select an option

Save Elsayegh/62b1e51b9b1c508daa803d28f82150c1 to your computer and use it in GitHub Desktop.
C++ 7-2 Calculating Rainfall - Total, Average, Min and Max values in Array
/*Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles.
The program should calculate and display the total rainfall for the year,the average monthly rainfall, and the months with the highest
and lowest amounts. */
#include <iostream>
#include <conio.h>
#include <ctime>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
int main() {
double months[12];
double largest = 0;
double smallest = 0;
double total = 0;
double avg = 0;
for (int i = 0; i < 12; i++) {
cout << "Enter the rainfall amount in the month number " << i + 1 << " :" << endl;
cin >> months[i];
if (i < 0) {
cout << "Please enter positive number for the rainfall amount in the month number " << i + 1 << " :" << i;
cin >> months[i];
}
total += months[i];
}
smallest = months[0];
largest = months[0];
for (int i = 0; i < 12; i++) {
if (months[i] < smallest)
smallest = months[i];
if (months[i] > largest)
largest = months[i];
}
avg = total / 12;
cout << "Total rainfall for the whole year is: " << total << endl;
cout << "Average rainfall for the year is: " << avg << endl;
cout << "Largest amount of rainfall in the year is: " << largest << endl;
cout << "smallest amount of rainfall in the year is: " << smallest << endl;
_getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment