Ok, this was an assignment that I was deducted points due to the fact that the output was displayed backwards. I was supposed to prompt the user for input then display the input in descending order, but I am having an issue with the display. I have two arrays, one year[] to hold the months and one month[totalMonths] to hold the user input. When i sort and display the input the year[] does not correspond with the months, it is fixed. So for example if the user enters 1 for Jan, 2 for Feb and 3 for Mar, the display would be;
Jan: 3
Feb: 2
Mar: 1
Any ideas on how i can get the months to correspond with the its proper input for the display? Here is the sort and display function:
void sortArray(double month[], string year[], int totalMonths)
{
int temp;
bool swap;
do
{
swap = false;
for(int count = 0; count < totalMonths - 1; count++)
{
if(month[count] < month[count + 1])
{
temp = month[count];
month[count] = month[count + 1];
month[count + 1] = temp;
swap = true;
}
}
} while(swap);
cout << "------------------------------------------------------------" << endl;
cout << "Here are the months rainfall statistics sorted from highest to lowest: " << endl;
for (int index = 0; index < totalMonths; index++)
cout << year[index] << "\t " << setw(5) << month[index] << endl;
}
Here is my string year[] definition:
string year[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Are you allowed to rearrange the
yeararray? In your sort routine, where you swap themonthvalues, you can swap the corresponding values in theyeararray.If you don’t want to mutate the
yeararray you can just add a level of indirection. Define an array of indices into themonthandyeararrays and sort the indices.