Hi everyone, was wondering if maybe someone could help me. I'm new to coding and have an assignment to do. The assignment is as follows:
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 the year, the average monthly rainfall, and the months with the highest and lowest amounts.
The program should display a list of months, sorted in order of rainfall, from highest to lowest
Input Validation: Do not accept negative numbers for monthly rainfall figures.
Include the following functions
Calculate total rainfall
Calculate the average rainfall
Find the largest amount of rainfall
Find the smallest amount of rainfall
Sort the array of rainfall data
Display the sorted data.
Display Array
what I have so fa
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Months, double, things needed
const int MONTHS = 12; //Number of elements
string name[MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
int i = 0; //Loop index
double rainFall[MONTHS];
double total = 0, avg = 0, highRain = 0, lowRain = 0;
string highMonth;
string lowMonth;
int main()
{
for (int i = 0; i < MONTHS; i++)
{
//input total rainfall for each month
cout << "Enter rainfall for " <<name[i] << ": " << endl;
cin >> rainFall[i];
// input validation -- negative inputs not allowed
while (rainFall[i] < 0)
{
//notify user of the error
cout << "invalid data (negative rainfall) -- retry" << endl;
//get new input
cin >> rainFall[i];
}
//total rainfall
total += rainFall[i];
}
//average rainfall
avg = total / 12;
// Max or most rainfall
highRain = rainFall[0];
highMonth = name[0];
for (int i = 1; i < MONTHS; i++)
{
if (rainFall[i] > highRain)
{
highMonth = name[i];
highRain = rainFall[i];
}
}
//least or less rainfall
lowRain = rainFall[0];
lowMonth = name[0];
for (int i = 1; i < MONTHS; i++)
{
if (rainFall[i] < lowRain)
{
lowMonth = name[i];
lowRain = rainFall[i];
}
}
// Display calculations --> average and total rainfall, most and least rainfall
cout << "\n";
cout << "Total rainfall: " << total << endl;
cout << "Average: " << avg << endl;
cout << "Most rainfall in " << highMonth << " with an amount of " << highRain << endl;
cout << "Least rainfall in " << lowMonth << " with an amount of " << lowRain << endl;
cout << "\n";
// Display a list of months, sorted in order of rainfall, from high to low
for (i = 0; i < MONTHS; i++) {
cout << name[i] << " " << rainFall[i] << endl;
}
return 0;
}
I was wondering if anyone knew how to go about sorting it? I just cant seem to wrap my head about it.
Thank you in advance!