Need assistance with sorting my code

Hello there.

I have put together a program that displays rainfall in each of the twelve months but I am having a hard time sorting the months from most rainfall to least rainfall. Any help is appreciated. Thank you.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

//Function Prototypes
double getTotal(double[], int);
double getAverage(double[], int);
double getHighest(double[], int);
double getLowest(double[], int);

int main()

{
int count; // loop to get vaue for each month
const int Size = 12; // 12 months in a year

double RainAmt[Size];
int RAINHIGHEST, RAINLOWEST; // Highest and lowest months
double RAINTOTAL, RAINAVERAGE; // Total and average rainfall
string Month[] = { "January", "Febuary", "March", "April", // String names for all twelve months
"May", "June", "July", "August", "September", "October",
"November", "December" };


cout << "Please enter the average rainfall for the month provided " << endl; // Get user input
for (count = 0; count < Size; count++)
{
cout << Month[count] << " : ";
cin >> RainAmt[count];
while (RainAmt < 0)
{
cout << "Positive numbers only. Please enter a positive number for " << Month[count] << endl; // Input validation
cin >> RainAmt[count];
}
}

RAINTOTAL = getTotal(RainAmt, Size);
RAINAVERAGE = getAverage(RainAmt, Size);

string Low_Month, High_Month; // Array of strings
double Low_Point, High_Point; // Array of doubles

RAINLOWEST = getLowest(RainAmt, Size);
Low_Month = Month[RAINLOWEST];
Low_Point = RainAmt[RAINLOWEST];

RAINHIGHEST = getHighest(RainAmt, Size);
High_Month = Month[RAINHIGHEST];
High_Point = RainAmt[RAINHIGHEST];

cout << endl << endl;

cout << "The total rainfall of the year was " << RAINTOTAL << " inches. "<< endl; // Displays total rainfall
cout << endl;
cout << "The average rainfall of the year was " << RAINAVERAGE << " inches. " << endl; // Displays average rainfall
cout << endl;
cout << Low_Month << " had the least rainfall of the year with " << Low_Point << " inches. " << endl; // Displays least rainfall of the year
cout << endl;
cout << High_Month << " had the most rainfall of the year with " << High_Point << " inches. " << endl; // Displays most rainfall of the year
cout << endl;
return 0;
}

double getTotal(double RainAmt[], int size)
{
double Total = 0;
for (int count = 0; count < size; count++)
{
Total += RainAmt[count];
}
return Total;
}

double getAverage(double RainAmt[], int size)
{
double Total = 0;
double Average;
for (int count = 0; count < size; count++)
{
Total += RainAmt[count];
}
Average = Total / size;

return Average;
}
double getLowest(double RainAmt[], int size)
{
double Low_Point = RainAmt[0];
int Low_Count = 0;

for (int count = 0; count < size; count++)
{
if (RainAmt[count] <= Low_Point)
{
Low_Count = count;
Low_Point = RainAmt[count];
}
}
return Low_Count;
}

double getHighest(double RainAmt[], int size)
{
double High_Point = RainAmt[0];
int High_Count = 0;

for (int count = 0; count < size; count++)
{
if (RainAmt[count] >= High_Point)
{
High_Count = count;
High_Point = RainAmt[count];
}
}
return High_Count;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
double getLowest(double RainAmt[], int size)
{
double Low_Point = RainAmt[0];
int Low_Count = 0;

for (int count = 0; count < size; count++)
{
if (RainAmt[count] <= Low_Point)
{
Low_Count = count;
Low_Point = RainAmt[count];
}
}
return Low_Count;
}

double getHighest(double RainAmt[], int size)
{
double High_Point = RainAmt[0];
int High_Count = 0;

for (int count = 0; count < size; count++)
{
if (RainAmt[count] >= High_Point)
{
High_Count = count;
High_Point = RainAmt[count];
}
}
return High_Count;
}


Should be :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
double getLowest(double RainAmt[], int size)
{
	int i;
	int lowest_idx = 0;
	double lowest = RainAmt[0];
	for (i = 1; i < size; i++)
	{
		if(RainAmt[i] < lowest)
		{
			lowest = RainAmt[i];
			lowest_idx = i;
		}
	}
	return lowest_idx;
}

double getHighest(double RainAmt[], int size)
{
	int i;
	int highest_idx = 0;
	double highest = RainAmt[0];
	for (i = 1; i < size; i++)
	{
		if(RainAmt[i] > highest)
		{
			highest = RainAmt[i];
			highest_idx = i;
		}
	}
	return highest_idx;
}
Thank you, I implemented the changes. Advice on how to get it to display the months in descending order, most rainfall to the least? I am messing around with something but not getting the results I am looking for.
Topic archived. No new replies allowed.