好的,這是一個由於輸出反向顯示而被扣除的分配。我應該提示用戶輸入,然後按降序顯示輸入,但我遇到了顯示問題。我有兩個數組,一年[]來保存月份和一個month[totalMonths]
來保存用戶輸入。當我排序和顯示輸入year[]
不符合月份,它是固定的。例如,如果用戶在Jan輸入1,在Feb輸入2,在Mar輸入3,則顯示爲; 揚:3 月:2 三月:1排序輸入反向工作
如何我能得到個月與對顯示其應有的輸入對應任何想法?這裏是排序和顯示功能:
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;
}
這裏是我的string year[]
定義:
string year[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
我suppost你的年數組包含諸如:{「jan」,「feb」,「mar」,...}?你可以打印你的年份表定義plz – grifos 2012-03-26 16:32:56
我剛在我的文章中更新了我的年份[]定義。 – Gmenfan83 2012-03-26 16:35:23
這將最大的元素放在頂部;當你交換你應該交換的月份和年份(以及任何其他領域你綁在一起) – Adrian 2012-03-26 17:02:37