I wrote this program last semester: Its to calculate inputs of each months rainf
ID: 667107 • Letter: I
Question
I wrote this program last semester: Its to calculate inputs of each months rainfall, find the avg, etc. but now I have to be able to make a string of the months in ascending order of their rainfall. I have been working on it all week and I just cant seem to get it right. But this is my main code and now I need a code to be able to take the users inputted rainfalls and calculate the months in ascending order. C++. (This is my second 8-week course so I am still new to this.) If possible can there be comments so I can make sure I understand what I am doing wrong.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
double getTotal(double monthlyRainfall[], int n);
double getAverage(double monthlyRainfall[], int n);
string getHighest(double monthlyRainfall[], string months[], int n);
string getLowest(double monthlyRainfall[], string months[], int n);
int main()
{
double totalRainfallInput = 0.0; //Initializing the variable
double avgRainfall = 0.0; //Initializing the variable
string minRainfallMonth;
string maxRainfallMonth;
const int numberMonths = 12;
string months[numberMonths] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double total[numberMonths];
cout << " Welcome to Christina's Yearly Rainfall Calculator!" << endl;
cout << "_____________________________________________" << endl;
for (int i = 0; i<numberMonths; i++)
{
cout << "Please enter the rainfall amount for " << months[i] << ": ";
cin >> total[i];
while (total[i] < 0)
{
cout << "Please enter a valid number. Negative numbers are not valid. Re-enter data for " << months[i] << ": ";
cin >> total[i];
}
}
//Required Functions
totalRainfallInput = getTotal(total, numberMonths);
avgRainfall = getAverage(total, numberMonths);
maxRainfallMonth = getHighest(total, months, numberMonths);
minRainfallMonth = getLowest(total, months, numberMonths);
//Display the data
cout << fixed << setprecision(2) << showpoint;
cout << "____________________________________________" << endl;
cout << " The total rainfall for the year is: " << totalRainfallInput;
cout << " The average rainfall for the year is: " << avgRainfall;
cout << " The month that had the least amount of rainfall was: " << minRainfallMonth;
cout << " The month that had the most amount of rainfall was: " << maxRainfallMonth << endl;
}
//Calculations to find the total rainfall, average, max and min rainfall
double getTotal(double monthlyRainfall[], int numberMonths)
{
double totalRainfall = 0.0; //Initializing the variable
for (int count = 0; count<numberMonths; count++)
totalRainfall += monthlyRainfall[count];
return totalRainfall;
}
double getAverage(double monthlyRainfall[], int numberMonths)
{
double totalRainfall = 0.0; //Initializing the variable
for (int count = 0; count<numberMonths; count++)
totalRainfall += monthlyRainfall[count]; //totalRainfall=totalRainfall+monthlyRainfall[count]
return totalRainfall / numberMonths;
}
string getHighest(double monthlyRainfall[], string months[], int numberMonths)
{
double max = monthlyRainfall[0];
int index = 0;
for (int count = 0; count<numberMonths; count++)
if (monthlyRainfall[count]>max)
{
max = monthlyRainfall[count];
index = count;
}
return months[index];
}
string getLowest(double monthlyRainfall[], string months[], int numberMonths)
{
double min = monthlyRainfall[0];
int index = 0;
for (int count = 0; count<numberMonths; count++)
if (monthlyRainfall[count]<min)
{
min = monthlyRainfall[count];
index = count;
}
return months[index];
}