What am I doing wrong with this sorting algorithm? The months aren't being displayed properly.
Code:
#include <iostream>
#include <string>
using namespace std;
void sort(int arry[], int size);
int main()
{
string months[] = {"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
int rainfall[12];
int highest = INT_MIN;
int lowest = INT_MAX;
int high;
int low;
cout << "Enter the monthly rainfall for each month" << endl;
for (int i = 0; i < 12; i++)
{
cout << months [i] << ": ";
cin >> rainfall[i];
if (cin.fail() || rainfall[i] < 0)
{
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Error...re-enter..." << endl;
cout << months [i] << ": ";
cin >> rainfall[i];
}
if (rainfall[i] > highest)
{
highest = rainfall[i];
high = i;
}
if (rainfall[i] < lowest)
{
lowest = rainfall[i];
low = i;
}
sort(rainfall, 12);
}
cout << "\nThe month with the highest rainfall is "
<< months[high] << endl;
cout << "\nThe month with the lowest rainfall is "
<< months[low] << endl;
cout << "\nHere are the months sorted from highest rainfall"
<< " to smallest rainfall:" << endl;
for (i = 0; i < 12; i++)
cout << months[high--] << endl;
return 0;
}
void sort(int array[], int size)
{
int swap;
int temp;
do
{
swap = 0;
for (int i = 0; i < size - 1; i++)
{
if (array[i] > array[i +1])
{
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swap = 1;
}
}
} while (swap != 0);
}