menu
bartleby
search
close search
Hit Return to see all results

Rainfall StatisticsWrite a program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order):the total rainfall for the year,the average monthly rainfall,and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November, December. Input Validation: Do not accept negative numbers for monthly rainfall figures. When a negative value is entered, the program outputs "invalid data (negative rainfall) -- retry" and attempts to reread the value. NOTE: Decimal values should be displayed using default precision, i.e. do not specify precision.

Question

Rainfall Statistics

Write a program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order):



  • the total rainfall for the year,
  • the average monthly rainfall,
  • and the months with the highest and lowest amounts.

 


Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November, December.

 


Input Validation: Do not accept negative numbers for monthly rainfall figures. When a negative value is entered, the program outputs "invalid data (negative rainfall) -- retry" and attempts to reread the value.

 

NOTE: Decimal values should be displayed using default precision, i.e. do not specify precision.

check_circleAnswer
Step 1

The following program is in C++ language and it is compiled in the “repl.it” compiler.

Program plan:

  • Include the required library files.
  • Define a “main ()” function.
    • Declare the required variables.
    • Declare and initialize an array for months.
    • Get the rainfall input from the user.
    • And validate the input.
    • Then compute total rainfall and average rainfall.
    • Determine the highest and lowest rainfall month.
Step 2

Program:

// Include required library files
#include <iostream>
#include <string>

using namespace std;

// Definition of main function
int main()
{

// Declare constant variable
constint ALLMONTHS = 12;

// Declare the required variables
double highest, lowest, getAvg;
double tot = 0;

// Array creation of doubles
double rainfall[ALLMONTHS];

// Array creation of string
string months[ALLMONTHS] = { "January", "February", "March", "April","May", "June", "July", "August", "September","October", "November", "December" };

// for loop to get total rainfall
for ( int month = 0; month < ALLMONTHS; month++ )
{

// Get the rainfall input
cout << "\nThe rainfall for the month of " << months[month] << ": ";

// Store it into the array
cin >> rainfall[month];

// Calculate the total rainfall for all month
tot += rainfall[month];

// while to validate the input
while (rainfall[month] < 0)
{

// Print the invalid message
cout << "\nInvalid data(Negative rainfall)-- retry";

// Get the valid input
cout << "\nPlease enter positive amount for " << months[month] << " again: ";

// Store it into the array
cin >> rainfall[month];

// Calculate the total rainfall for all month
tot += rainfall[month];
}
}

// Print the total rainfall
cout << "\nThe total rainfall was: \t" << tot << endl;

// Compute the Average rainfall
getAvg = tot / ALLMONTHS;

// Print the average rainfall
cout << "\nThe average rainfall was: \t" << getAvg << endl;

// Declare the string variables
string maxMonth, minMonth;

// for loop to iterate the months
for ( int month = 0; month < ALLMONTHS; month++ )
{

// Assign rainfall[0] to the highest
highest = rainfall[0];

// For loop to get the highest rainfall month
for ( int count = 0; count < ALLMONTHS; co...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Our solutions are written by experts, many with advanced degrees, and available 24/7

See Solution
Tagged in

Engineering

Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Assume that a computer architect has already designed 6 two-address and 24 zero-address instructions...

A: Given:Instruction length = 11 bits = 211 = 2048 bitsAddress register size = 4 bits5 two-address inst...

question_answer

Q: Construct an npda corresponding to the grammar S→aABB|aAA,A→aBB|b,B→bBB|A.

A: Firstly, we will convert the given grammar into GNFS → aABB|aAAA → aBB|bB → bBB|aBB|a

question_answer

Q: How to implement a pluggable Adapter design pattern in Java?

A: Let’s say we have a client class called Client.java which calls method1( ) of an existing library cl...

question_answer

Q: Find regular grammars for the following languages on {a, b}: L = {w : na (w) and nb (w) are both eve...

A: Regular grammar for the language L = {W: na (w) and nb (w) are both even} is as follows:

question_answer

Q: Find and list the websites of the parent and child standardization organizations that create standar...

A: Standards in Data communication and computer networking defines the rules which are needed for inter...

question_answer

Q: Construct a dfa that accepts the language generated by the grammar S → abA, A → baB, B → aA|bb.

A: Deterministic finite automata (DFA):

question_answer

Q: 12 4x)x,is an another form of representation of fuzzy set. A, (x) /x, is an another form of represen...

A: A fuzzy set is a pair X, m(A)) where X is a Universal set and mA) is a membership function or degree...

question_answer

Q: Write a program that contains 2 methods, main and printLine (10 points)1) The printLine method will ...

A: Declare required variables ‘first’, ‘second’ and ‘times’ which is used to store first character, sec...

question_answer

Q: C++ Thanks!Example:"apple"-&gt;NULLreturn "apple"-&gt;NULL"apple"-&gt;"apple"-&gt;NULLreturn "apple"...

A: So in the fisrt step we will make the current node value is head.node* current = head;

Sorry about that. What wasn’t helpful?