In: Computer Science
Write a C++ console application that allows your user to enter the total rainfall for each of 12 months into an array of doubles. The program should validate user input by guarding against rainfall values that are less than zero. After all 12 entries have been made, the program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts.
#include <iostream> #include <string> using namespace std; int main() { string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; double rainfalls[12]; for (int i = 0; i < 12; ++i) { while (true) { cout << "Enter rainfall for " << months[i] << ": "; cin >> rainfalls[i]; if (rainfalls[i] >= 0) break; cout << "Invalid input. Try again!" << endl; } cout << endl; } double total = 0; int min = 0, max = 0; for (int i = 0; i < 12; ++i) { total += rainfalls[i]; if (rainfalls[i] > rainfalls[max]) max = i; if (rainfalls[i] < rainfalls[min]) min = i; } cout << "total rainfall for the year is " << total << endl; cout << "average monthly rainfall is " << total / 12 << endl; cout << "highest rainfall is " << rainfalls[max] << " on Month of " << months[max] << endl; cout << "lowest rainfall is " << rainfalls[min] << " on Month of " << months[min] << endl; return 0; }