Reputation: 2587
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"};
Upvotes: 1
Views: 172
Reputation: 587
Only thing you have to change month[count] < month[count + 1])
to month[count] > month[count + 1])
. So your complete code will be given below:
#include<iostream>
#include<string>
#include<string.h>
#include<iomanip>
using namespace std;
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;
}
int main()
{
string year[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
double month[] = {12,11,10,9,8,7,6,5,4,3,2,1};
sortArray(month,year,12);
return 0;
}
Upvotes: 0
Reputation: 18652
Are you allowed to rearrange the year
array? In your sort routine, where you swap the month
values, you can swap the corresponding values in the year
array.
If you don't want to mutate the year
array you can just add a level of indirection. Define an array of indices into the month
and year
arrays and sort the indices.
int index[12] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
// inside your sort routine...
if(month[index[count]] < month[index[count + 1]])
{
temp = index[count];
index[count] = index[count + 1];
index[count + 1] = temp;
swap = true;
}
// print the arrays...
for (int count = 0; count < totalMonths; count++)
cout << year[index[count]] << "\t " << setw(5) << month[index[count]] << endl;
Upvotes: 1
Reputation: 3381
Like Blastfurnace pointed out you have to sort your year array to match the month. Or if you can't, you can create a small struct to represent your month data. Like this:
typedef struct _monthData{
double data;
int monthIndex;
} monthData;
void sortArray(monthData month[], string year[], int totalMonths)
{
int temp;
bool swap;
do
{
swap = false;
for(int count = 0; count < totalMonths - 1; count++)
{
if(month[count].data < month[count + 1].data)
{
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[month[index].monthIndex] << "\t " << setw(5) << month[index] << endl;
}
Regards
Upvotes: 2