Question

In: Computer Science

Write a C++ program that lets the user enter the total rainfall for each of 12...

Write a C++ 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.

Use an Array for saving the Month names and values.

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.

Sample Output:

Enter rainfall for January: 1
Enter rainfall for February: 2
Enter rainfall for March: 3
Enter rainfall for April: 4
Enter rainfall for May: 5
Enter rainfall for June: 6
Enter rainfall for July: 7
Enter rainfall for August: 8
Enter rainfall for September: 9
Enter rainfall for October: 10
Enter rainfall for November: 11
Enter rainfall for December: 12
Total rainfall: 78
Average rainfall: 6.5
Least rainfall in: January
Most rainfall in: December

Solutions

Expert Solution

#include <iostream>

using namespace std;

int main()

{

    // Use an Array for saving the Month names and values.

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

    double rainfall[12];

    // average, max and min, total rainfall

    double average = 0, total = 0;

    int max = 0, min = 0;

    for (int i = 0; i < 12; i++)

    {

        cout << "Enter rainfall for " << months[i] << ": ";

        cin >> rainfall[i];

        // When a negative value is entered,

        if (rainfall[i] < 0)

        {

            cout << "invalid data (negative rainfall) -- retry";

            // attempts to reread the value.

            i--;

        }

        else

        {

            // compute min

            if (rainfall[min] > rainfall[i])

                min = i;

            // compute max

            if (rainfall[max] < rainfall[i])

                max = i;

            // compute total

            total += rainfall[i];

        }

    }

    // compute average

    average = total / 12;

    cout << "Total rainfall: " << total << endl;

    cout << "Average rainfall: " << average << endl;

    cout << "Least rainfall in: " << months[min] << endl;

    cout << "Most rainfall in: " << months[max] << endl;

}

.

Output:

.


Related Solutions

Design a program that lets the user enter the total rainfall for each of 12 months...
Design a program that lets the user enter the total rainfall for each of 12 months into an array. 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. Requirements You must use the given function prototypes. You can add more functions if you like. def get_total_rainfall(months, months_len): def get_average_monthly_rainfall(months, months_len): def get_month_with_highest_rainfall(months, months_len): def get_month_with_lowest_rainfall(months, months_len): Design Considerations Use a global parallel array of...
Design a program that lets the user enter the total rainfall for each of 12 months...
Design a program that lets the user enter the total rainfall for each of 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the months with the highest and lowest amounts. This is my Pthon 3.8.5 code so far: #Determine Rainfall Program for a 12 month Period #It should calculate and output the "total yearly rainfall, avg monthly rainfall, the months with highest and lowest amounts #list of...
design a program that lets the user enter the total rainfall for each of 12 months...
design a program that lets the user enter the total rainfall for each of 12 months into an array. 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.   ---->>> Enhance the program so it sorts the array in ascending order and displays the values it contains. You can choose which sorting algorithm to use (bubble sort, selection sort, or insertion sort). Depending on your...
Design a program that lets the user enter the total rainfall for each of 12 months...
Design a program that lets the user enter the total rainfall for each of 12 months into an array. 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. Design a program to solve Chapter 8 Programming Exercise 3 (Rainfall Statistics) in your textbook. Create parallel arrays for the month names and rainfall amounts for each month. Use month names (i.e. January, February, March, etc.)...
In Python: Design a program that lets the user enter the total rainfall for each of...
In Python: Design a program that lets the user enter the total rainfall for each of the 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the months with the highest and lowest amounts. However, to start, do not ask the user for input. Instead, hard code the monthly rainfalls as a list in your program, and then use the list to determine how to conduct the processing...
In python please design a program that lets the user enter the total rainfall for each...
In python please design a program that lets the user enter the total rainfall for each of the last 10 years into an array. The program should calculate and display the total rainfall for the decade, the average yearly rainfall, and the years with the highest and lowest amounts. Should have Indentation Comments Good Variables initializations Good questions asking for the parameters of the code Output display with explanation Screenshot
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
DESIGN A FLOWCHART IN FLOWGORITHM Rainfall Statistics Design a program that lets the user enter the...
DESIGN A FLOWCHART IN FLOWGORITHM Rainfall Statistics Design a program that lets the user enter the total rainfall for each of 12 months into an array. 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. PLEASE AND THANK YOU
C++ Write a program that lets the user enter a two letters which are f and...
C++ Write a program that lets the user enter a two letters which are f and s with a length of 5. And outputs how many times it was occurred and lists the 2 most repeating pattern with 5lengths of f and s. The output display should always start at three-f(s) .Include an option where user can retry the program. Example: Input: sssfsfsfssssfffsfsssssfffsffffsfsfssffffsfsfsfssssfffffsffffffffffffssssssssfffsffffsssfsfsfsfssssfffsssfsfsffffffssssssffffsssfsfsfsss Output: The most repeating 5lengths of pattern is: fffsf and occurred 6times. Output2: The second most repeating...
Write a program in C that lets the user enter a message using SMS Language(e.g. lol,...
Write a program in C that lets the user enter a message using SMS Language(e.g. lol, omg, omw etc.), then translates it into English (e.g. laughing out loud, oh my god, on my way etc.). Also provide a mechanism to translate text written in English into SMS Language. needs to be able to translate at least 10 sms words
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT