168k views
4 votes
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.

User A Das
by
7.6k points

1 Answer

6 votes

Answer:

Step-by-step 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:\\";

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

{

double amt;

cin >> amt;

rainfalls.push_back(amt);

}

cout << "\\MONTHLY RAINFALL AMOUNTS\\";

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 << "\\AVERAGE RAINFALL FOR THE YEAR\\" << "Average: " << getAverage(rainfalls) << endl;

int minIndex = getMinimum(rainfalls);

int maxIndex = getMaximum(rainfalls);

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

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

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

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

return 0;

}

User RBV
by
7.3k points