Question
The following code that I pasted below is from a program I had written in C++. Can you please add new C++ source code to
The following code that I pasted below is from a program I had written in C++. Can you please add new C++ source code to this existing code so that the program displays a list of months, sorted in the order of the rainfall from the highest to the lowest?
#include
#include
#include
using namespace std;
double getTotal(double[], int); // Function for total rainfall
double getAverage(double[], int); // Function for rainfall over the given months.
double getLowest(double[], int, int&); // Returns the lowest value, provides the index of the
// lowest value in the last parameter.
double getHighest(double[], int, int&); // Returns the highest value, provides the index of the
// highest value in the last parameter.
int main()
{
const int SIZE = 12; // For the given months.
string months[SIZE] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" };
double rainfall[SIZE], // Store monthly rainfall.
total, // Total of all rainfall.
average; // Monthly average of rainfall.
int lowest,
highest;
int i;
for (i = 0; i < SIZE; i++) // Initialize counter and capture monthly values for rainfall.
{
cout << "What is the rainfall amount in inches for the month of " << months[i] << "? " << endl;
cin >> rainfall[i];
while (rainfall[i] < 0) // Validate rainfall is a positive number.
{
cout << "Rainfall must be greater than or equal to 0 inches. Please re-enter: " << endl;
cin >> rainfall[i];
}
}
total = getTotal(rainfall, SIZE);
cout << "The total rainfall, in inches, for the year is: " << total << endl;
average = getAverage(rainfall, SIZE);
cout << "The average monthly rainfall, in inches, is: " << average << endl;
lowest = getLowest(rainfall, SIZE, i);
cout << "The lowest amount of rainfall is " << rainfall[lowest] << ", which took place in " << months[lowest] << endl;
highest = getHighest(rainfall, SIZE, i);
cout << "The highest amount of rainfall is " << rainfall[highest] << ", which took place in " << months[highest] << endl;
return 0;
}
// Function definition to add monthly rain totals for the year.
double getTotal(double rainfall[], int SIZE)
{
double total = 0;
for (int i = 0; i < SIZE; i++)
total += rainfall[i];
return total;
}
// Function definition to divide the total rainfall by months.
double getAverage(double rainfall[], int SIZE)
{
double average = 0;
double total;
total = getTotal(rainfall, SIZE);
average = total / SIZE;
return average;
}
// Function definition to calculate the lowest rainfall data.
double getLowest(double rainfall[], int SIZE, int &i)
{
double lowest;
lowest = 0;
for (i = 1; i < SIZE; i++)
{
if (rainfall[i] < lowest)
lowest = i;
}
return lowest;
}
// Function definition to calculate the highest rainfall data.
double getHighest(double rainfall[], int SIZE, int &i)
{
double highest;
highest = 0;
for (i = 1; i < SIZE; i++)
{
if (rainfall[i] > highest)
highest = i;
}
return highest;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started