Write a program that lets the user enter the total rainfall for each of 12 months into a vector of doubles. The program will also have a vector of 12 strings to hold the names of the months. 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.

Respuesta :

Answer:

Explanation:

#include<iostream>

#include<iomanip>

#include<vector>

using namespace std;

double getAverage(const vector<double> amounts)

{

   double sum = 0.0;

   for (int i = 0; i < amounts.size(); i++)

       sum += amounts[i];

   return(sum / (double)amounts.size());

}

int getMinimum(const vector<double> amounts)

{

   double min = amounts[0];

   int minIndex = 0;

   for (int i = 0; i < amounts.size(); i++)

   {

       if (amounts[i] < min)

       {

           min = amounts[i];

           minIndex = i;

       }

   }

   return minIndex;

}

int getMaximum(const vector<double> amounts)

{

   double max = amounts[0];

   int maxIndex = 0;

   for (int i = 0; i < amounts.size(); i++)

   {

       if (amounts[i] > max)

       {

           max = amounts[i];

           maxIndex = i;

       }

   }

   return maxIndex;

}

int main()

{

   vector<string> months;

   vector<double> rainfalls;

   months.push_back("January");

   months.push_back("February");

   months.push_back("March");

   months.push_back("April");

   months.push_back("May");

   months.push_back("June");

   months.push_back("July");

   months.push_back("August");

   months.push_back("September");

   months.push_back("October");

   months.push_back("November");

   months.push_back("December");

   cout << "Input 12 rainfall amounts for each month:\n";

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

   {

       double amt;

       cin >> amt;

       rainfalls.push_back(amt);

   }

   cout << "\nMONTHLY RAINFALL AMOUNTS\n";

   cout << setprecision(2) << fixed << showpoint;

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

       cout << left << setw(11) << months[i] << right << setw(5) << rainfalls[i] << endl;

   cout << "\nAVERAGE RAINFALL FOR THE YEAR\n" << "Average: " << getAverage(rainfalls) << endl;

   int minIndex = getMinimum(rainfalls);

   int maxIndex = getMaximum(rainfalls);

   cout << "\nMONTH AND AMOUNT FOR MINIMUM RAINFALL FOR THE YEAR\n";

   cout << months[minIndex] << " " << rainfalls[minIndex] << endl;

   cout << "\nMONTH AND AMOUNT FOR MAXIMUM RAINFALL FOR THE YEAR\n";

   cout << months[maxIndex] << " " << rainfalls[maxIndex] << endl;

   return 0;

}