2012-03-26 41 views
1

好的,這是一個由於輸出反向顯示而被扣除的分配。我應該提示用戶輸入,然後按降序顯示輸入,但我遇到了顯示問題。我有兩個數組,一年[]來保存月份和一個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"}; 
+0

我suppost你的年數組包含諸如:{「jan」,「feb」,「mar」,...}?你可以打印你的年份表定義plz – grifos 2012-03-26 16:32:56

+0

我剛在我的文章中更新了我的年份[]定義。 – Gmenfan83 2012-03-26 16:35:23

+0

這將最大的元素放在頂部;當你交換你應該交換的月份和年份(以及任何其他領域你綁在一起) – Adrian 2012-03-26 17:02:37

回答

1

你被允許重新排列year陣列?在您的排序例程中,在交換month值時,可以交換year陣列中的相應值。

如果你不想改變year數組,你可以添加一個間接級別。在monthyear數組中定義一系列索引並對索引進行排序。

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; 
+0

謝謝!這工作完美。感謝大家的時間和幫助,非常感謝! – Gmenfan83 2012-03-26 17:17:59

2

Blastfurnace指出,你有你的排序一年陣列月份相匹配。 或者如果你不能,你可以創建一個小結構來表示你的月份數據。就像這樣:

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; 
} 

問候

0

你唯一需要改變month[count] < month[count + 1])month[count] > month[count + 1])。所以你的完整的代碼將給出如下:

#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; 
}