Hello , so this is the exercise :Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles. The program should calculate and display the total rainfall for a year, the average monthly rainfall, and the months with the highest and lowest amounts. The program should also display the list of months, sorted in order of rainfall from highest to lowest.
So far I need help in how to connect the string array or the month and connect it to the sorted array so it matches the months:
This is the code that I have:
#include <iostream>
#include <iomanip>
// tried to do a function that will sort my array but I could not figure it out
using namespace std;
int main()
{
double raindropSum = 0, raindropAverage = 0;
int counter ;
double raindrops[12];
string months[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
cout << " Enter the amount of rainfall for each month : " << endl;
for (counter = 0; counter <= 11; counter++)
{
cout << months[counter] << " : ";
cin >> raindrops[counter];
while (raindrops[counter] < 0)
{
cout << "Raindrops cannot be negative enter a positive input " << months[counter] << endl;
cin >> raindrops[counter];
raindropSum = raindropSum + raindrops[counter];
raindropAverage = (raindropSum / 12) ;
cout << months[counter] << setprecision(3) << setw(2) << " " << raindrops[counter] << endl;
}
}
for( counter = 0 ; counter <= 11; counter++ )
{
raindropSum = raindropSum + raindrops[counter];
raindropAverage = (raindropSum / 12) ;
}
cout << " The List of the Months with their raindrop is here :" << endl;
cout << "-----------------------------------------------------------------------------------------------" << endl;
cout << " The sum of the raindrops in the year is " << raindropSum << endl;
cout << "-----------------------------------------------------------------------------------------------" << endl;
cout << " The average of rain drops for the whole year is " << raindropAverage << endl;
cout << "-----------------------------------------------------------------------------------------------" << endl;
cout << " The Lowest Month was " << " lowest month should be here" << " with a total of " << raindrops[0] << " raindrops" << endl;
cout << "-----------------------------------------------------------------------------------------------" << endl;
cout << " The Highest Month was " << " " << " highest month should be here " << raindrops[11] << " raindrops " << endl;
cout << "-----------------------------------------------------------------------------------------------" << endl;
return 0;
}
Have you ever seen: std::pair<std::string,double> months[12];
Now an element in the array is not a double, but a pair that holds both a name and the rainfall.
The Standard Library has a sorting algorithm. It could be used like this:
1 2 3 4
using P = std::pair<std::string,double>;
P months[12];
// set values of months
std::sort( months, months+12, [](P lhs, P rhs){ return rhs.second < lhs.second; } );
What is essential in that? Two things:
1. The sort algorithm swaps whole elements regardless how we want to order them.
2. The question about some pair of elements is, are these two in correct order?
Lets get back to two separate arrays.
* You do know that you have to swap something, if the rainfall is not in descending order.
* Logical "whole element" includes an element from each array (double and string).