Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I wrote this program last semester: Its to calculate inputs of each months rainf

ID: 667108 • 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 add the selection sort within it so after it caclulates all the information, it then needs to show each of the months in ascending order of how much rainfall occured during that month. 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];
}

Explanation / Answer

I have added the selection sort function in your program with comments to make you undersand each step. The function definition is given at the top along with the other function definitions. Besides the main function calls the selectionsort function to sort the array containing values of the amounts of rainfalls in the various months. The for loop in the main function helps display the sorted array along with the respective months. Here is the following code:

#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);
void selectionsort(double total[],string months[],int n);       //function definition of the selectionsort function
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);
selectionsort(total,months,numberMonths);   //calling the sorting functions and passing two arrays to it along with the max no. of elements
                                          
//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;
cout<<" Months according to ascending order of rainfall are as follows : ";
for(int i=0;i<numberMonths;i++)       //loop to print the sorted array along with the coressponding months
{
cout<<months[i]<<" had ";
               cout<<total[i]<<" amount of rainfall ";
}
}
//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];
}
void selectionsort(double a[],string b[],int n) //selection sort function definition
{
int i,j,temp1;
   string temp2;
for(i=0;i<n;i++)               //two for loops to execute the selection sort algo
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i])         //comparing values of rainfalls and sorting them using temporary variable temp1
{                   //temp2 variable changes position of months in the array corresponding to changes in position of rainfalls in the array
   temp1=a[i];       //temp1 and temp2 variables are used to interchange values in the array if the if-condition turns out to be true
   temp2=b[i];      
  
a[i]=a[j];
b[i]=b[j];
  
a[j]=temp1;
b[j]=temp2;
}
}
}
}